Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- users_raw=$(w -hs)
- declare -a users
- declare -a user_sessions
- uindex=0
- # Aggregate current session data
- while read -r line; do
- uip=$(echo "$line" | egrep -o "([0-9]+.){3}[0-9]+" | sed "s/./_/g")
- u=$(echo "$line" | egrep -o "^[^ "$'t'"]+")
- u="$u@$uip"
- add=1
- for user in "${users[@]}"; do
- if [ "$user" == "$u" ]; then
- add=0
- break
- fi
- ((uindex++))
- done
- if [ "$add" -eq 1 ]; then
- users["${#users[@]}"]="$u"
- fi
- if [ "$uindex" -eq "${#user_sessions[@]}" ]; then
- user_sessions["$uindex"]=1
- else
- ((user_sessions["$uindex"]++))
- fi
- done <<< "$users_raw"
- # Output config info
- if [ "$1" = "config" ]; then
- echo 'graph_title User Sessions'
- echo 'graph_vlabel Sessions'
- echo 'graph_category system'
- echo 'graph_info This graph shows the number of sessions for each user per 5 minutes.'
- # This creates a unique field for each user/ip combo
- for u in "${users[@]}"; do
- uclean="$(echo -n $u | sed "s/_/./g")"
- echo "$u.label $uclean"
- echo "$u.info The number of sessions for user@ip $u."
- done
- exit 0
- fi
- # Finally, output the actual instantaneous value
- uindex=0
- for u in "${users[@]}"; do
- echo "$u.value ${user_sessions[$uindex]}"
- ((uindex++))
- done
Add Comment
Please, Sign In to add comment