Guest User

Untitled

a guest
May 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. users_raw=$(w -hs)
  4. declare -a users
  5. declare -a user_sessions
  6. uindex=0
  7.  
  8. # Aggregate current session data
  9. while read -r line; do
  10. uip=$(echo "$line" | egrep -o "([0-9]+.){3}[0-9]+" | sed "s/./_/g")
  11. u=$(echo "$line" | egrep -o "^[^ "$'t'"]+")
  12. u="$u@$uip"
  13. add=1
  14. for user in "${users[@]}"; do
  15. if [ "$user" == "$u" ]; then
  16. add=0
  17. break
  18. fi
  19. ((uindex++))
  20. done
  21.  
  22. if [ "$add" -eq 1 ]; then
  23. users["${#users[@]}"]="$u"
  24. fi
  25.  
  26. if [ "$uindex" -eq "${#user_sessions[@]}" ]; then
  27. user_sessions["$uindex"]=1
  28. else
  29. ((user_sessions["$uindex"]++))
  30. fi
  31. done <<< "$users_raw"
  32.  
  33. # Output config info
  34. if [ "$1" = "config" ]; then
  35.  
  36. echo 'graph_title User Sessions'
  37. echo 'graph_vlabel Sessions'
  38. echo 'graph_category system'
  39. echo 'graph_info This graph shows the number of sessions for each user per 5 minutes.'
  40.  
  41. # This creates a unique field for each user/ip combo
  42. for u in "${users[@]}"; do
  43. uclean="$(echo -n $u | sed "s/_/./g")"
  44. echo "$u.label $uclean"
  45. echo "$u.info The number of sessions for user@ip $u."
  46. done
  47.  
  48. exit 0
  49. fi
  50.  
  51. # Finally, output the actual instantaneous value
  52. uindex=0
  53. for u in "${users[@]}"; do
  54. echo "$u.value ${user_sessions[$uindex]}"
  55. ((uindex++))
  56. done
Add Comment
Please, Sign In to add comment