| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
#----------------------------------------------------------------- |
| 4 |
# i-scream host plugin - user information collection |
| 5 |
# $Author: tdb1 $ |
| 6 |
# $Id: statgrab.pl,v 1.46 2001/11/14 15:18:25 tdb1 Exp $ |
| 7 |
# |
| 8 |
# A short perl script to parse the output of the users command. |
| 9 |
#----------------------------------------------------------------- |
| 10 |
|
| 11 |
|
| 12 |
$| = 1; |
| 13 |
|
| 14 |
|
| 15 |
# You'd be silly not to use this ;) |
| 16 |
use strict; |
| 17 |
|
| 18 |
# Have to hope this will work really. |
| 19 |
my($ostype) = `uname -s`; chomp($ostype); |
| 20 |
|
| 21 |
# Decide which paths we should use. |
| 22 |
my($usersbin); |
| 23 |
if ($ostype eq "SunOS") { |
| 24 |
# covers: Solaris 7/8 |
| 25 |
$usersbin = "/usr/ucb/users"; |
| 26 |
} |
| 27 |
elsif ($ostype eq "Linux" || $ostype eq "FreeBSD") { |
| 28 |
# covers: Debian r2.2 |
| 29 |
# covers: FreeBSD 4.X |
| 30 |
$usersbin = "/usr/bin/users"; |
| 31 |
} |
| 32 |
else { |
| 33 |
print "i-scream_users.pl Error: Unable to identify system type - \"$ostype\".\n"; |
| 34 |
print "\"uname -s\" does not report one of the following known types;\n"; |
| 35 |
print " SunOS, Linux, FreeBSD\n"; |
| 36 |
exit(1); |
| 37 |
} |
| 38 |
|
| 39 |
# Run the following components: - |
| 40 |
&print_ident(); |
| 41 |
&include_users(); |
| 42 |
|
| 43 |
# End the program normally. |
| 44 |
exit(0); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
# prints out an identifier for this version of the script |
| 50 |
# this could be used in checks further downstream |
| 51 |
sub print_ident() { |
| 52 |
print 'packet.plugins.ident.i-scream_users i-scream_users.pl $Revision: 1.46 $'; |
| 53 |
print "\n"; |
| 54 |
} |
| 55 |
|
| 56 |
# sub to print pairs of data, separated by a single space character. |
| 57 |
# If the second argument is undefined, then the pair is still printed, |
| 58 |
# however, the value shall be displayed as the the 'default' value |
| 59 |
# if the passed value was undefined. |
| 60 |
sub print_pair($$$) { |
| 61 |
my($default, $name, $value) = @_; |
| 62 |
|
| 63 |
if (!defined $value) { |
| 64 |
$value = $default; |
| 65 |
} |
| 66 |
|
| 67 |
# Remove the trailing linefeed if we've not already done so. |
| 68 |
chomp($value); |
| 69 |
|
| 70 |
# print the pair of data with a space inbetween. |
| 71 |
print "$name $value\n"; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
# sub to find out the list of all usernames logged |
| 76 |
# in to the machine and how many there are. |
| 77 |
sub include_users() { |
| 78 |
|
| 79 |
# Find out all users on this machine. |
| 80 |
my($users) = `$usersbin`; |
| 81 |
$users = "\n" unless defined $users; |
| 82 |
chomp $users; |
| 83 |
my($users_count) = 0; |
| 84 |
$users_count++ while $users =~ /\w+/g; |
| 85 |
my($users_list) = $users." "; |
| 86 |
|
| 87 |
&print_pair(0, "packet.users.count", $users_count); |
| 88 |
&print_pair("unknown", "packet.users.list", $users_list); |
| 89 |
} |