Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input plugin for applications user usage #840

Closed
steverweber opened this issue Mar 12, 2016 · 2 comments
Closed

input plugin for applications user usage #840

steverweber opened this issue Mar 12, 2016 · 2 comments

Comments

@steverweber
Copy link

This script samples how many users are running applications of interest, ignores duplicates like how chrome spawns multiple processes.

this data aids are department to see what programs are used and where to spend money.

currently this script uses exec input plugin however this logic might be good to translate to a native input plugin like: telegraf/plugins/inputs/system/processes_count.go


#!/bin/bash

# USAGE:
# ./count_usage.sh "count_usage,host=$(hostname),group=mfcf" 'app_alias:app_exe_regex$'
# ./count_usage.sh "count_usage,host=$(hostname)" 'chromium:chromium$' 'firefox:firefox$' 'browsers:(chromium|firefox)$'

# OUTPUT:
# count_usage,host=mbp chromium=2,firefox=1,browsers=3

# DEV NOTES:
# because of comm 15char limit using `ps args`
# ps args can cause issues if it has spaces because cut truncates on them.
# might need sed -r , bsd uses -E however newer GNU seems to allow -E even tho undocumented

out="$1 "

ps=`ps -eo user,args --sort user | tr -s ' ' | cut -d ' ' -f 1-2 | sort | uniq`
for a in "${@:2}" ; do
    key=`cut -d ':' -f 1 <<< "$a"`
    regex=`cut -d ':' -f 2 <<< "$a"`
    value=`echo "$ps" | sed -n -E "\!$regex!p" | wc -l`
    out="${out}${key}=${value},"
done

# remove the extra ',' from the end of the string
out=`echo "$out" | sed 's/.$//'`

# output the measurement line
echo $out
@sparrc
Copy link
Contributor

sparrc commented Mar 14, 2016

to me this seems like a perfect use of the exec plugin, TBH, I'd rather just leave it at that

@sparrc sparrc closed this as completed Mar 14, 2016
@steverweber
Copy link
Author

most of the input plugins could be created as good examples for exec... The issue here is when the next person is looking to accomplish the same objective they wont have to reinvent the wheel.

Perhaps there should be a git repo of user created exec scripts..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants