| 1 |
tdb |
1.1 |
#!/usr/bin/perl -w |
| 2 |
|
|
|
| 3 |
tdb |
1.3 |
# |
| 4 |
|
|
# i-scream central monitoring system |
| 5 |
tdb |
1.4 |
# http://www.i-scream.org.uk |
| 6 |
tdb |
1.3 |
# Copyright (C) 2000-2002 i-scream |
| 7 |
|
|
# |
| 8 |
|
|
# This program is free software; you can redistribute it and/or |
| 9 |
|
|
# modify it under the terms of the GNU General Public License |
| 10 |
|
|
# as published by the Free Software Foundation; either version 2 |
| 11 |
|
|
# of the License, or (at your option) any later version. |
| 12 |
|
|
# |
| 13 |
|
|
# This program is distributed in the hope that it will be useful, |
| 14 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
|
|
# GNU General Public License for more details. |
| 17 |
|
|
# |
| 18 |
|
|
# You should have received a copy of the GNU General Public License |
| 19 |
|
|
# along with this program; if not, write to the Free Software |
| 20 |
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 |
|
|
# |
| 22 |
|
|
|
| 23 |
tdb |
1.1 |
#----------------------------------------------------------------- |
| 24 |
|
|
# i-scream host plugin - user information collection |
| 25 |
tdb |
1.2 |
# $Author: tdb $ |
| 26 |
tdb |
1.4 |
# $Id: i-scream_users.pl,v 1.3 2002/05/18 18:15:57 tdb Exp $ |
| 27 |
tdb |
1.1 |
# |
| 28 |
|
|
# A short perl script to parse the output of the users command. |
| 29 |
|
|
#----------------------------------------------------------------- |
| 30 |
|
|
|
| 31 |
|
|
|
| 32 |
|
|
$| = 1; |
| 33 |
|
|
|
| 34 |
|
|
|
| 35 |
|
|
# You'd be silly not to use this ;) |
| 36 |
|
|
use strict; |
| 37 |
|
|
|
| 38 |
tdb |
1.2 |
# Get the OS type from the args, or try towork it out |
| 39 |
|
|
my($ostype) = $ARGV[0]; |
| 40 |
|
|
$ostype = `uname -s` if not defined $ostype; |
| 41 |
|
|
chomp($ostype); |
| 42 |
tdb |
1.1 |
|
| 43 |
|
|
# Decide which paths we should use. |
| 44 |
|
|
my($usersbin); |
| 45 |
|
|
if ($ostype eq "SunOS") { |
| 46 |
|
|
# covers: Solaris 7/8 |
| 47 |
|
|
$usersbin = "/usr/ucb/users"; |
| 48 |
|
|
} |
| 49 |
|
|
elsif ($ostype eq "Linux" || $ostype eq "FreeBSD") { |
| 50 |
|
|
# covers: Debian r2.2 |
| 51 |
|
|
# covers: FreeBSD 4.X |
| 52 |
|
|
$usersbin = "/usr/bin/users"; |
| 53 |
|
|
} |
| 54 |
|
|
else { |
| 55 |
|
|
print "i-scream_users.pl Error: Unable to identify system type - \"$ostype\".\n"; |
| 56 |
|
|
print "\"uname -s\" does not report one of the following known types;\n"; |
| 57 |
|
|
print " SunOS, Linux, FreeBSD\n"; |
| 58 |
|
|
exit(1); |
| 59 |
|
|
} |
| 60 |
|
|
|
| 61 |
|
|
# Run the following components: - |
| 62 |
|
|
&include_users(); |
| 63 |
|
|
|
| 64 |
|
|
# End the program normally. |
| 65 |
|
|
exit(0); |
| 66 |
|
|
|
| 67 |
|
|
|
| 68 |
|
|
|
| 69 |
|
|
|
| 70 |
|
|
# sub to print pairs of data, separated by a single space character. |
| 71 |
|
|
# If the second argument is undefined, then the pair is still printed, |
| 72 |
|
|
# however, the value shall be displayed as the the 'default' value |
| 73 |
|
|
# if the passed value was undefined. |
| 74 |
|
|
sub print_pair($$$) { |
| 75 |
|
|
my($default, $name, $value) = @_; |
| 76 |
|
|
|
| 77 |
|
|
if (!defined $value) { |
| 78 |
|
|
$value = $default; |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
|
# Remove the trailing linefeed if we've not already done so. |
| 82 |
|
|
chomp($value); |
| 83 |
|
|
|
| 84 |
|
|
# print the pair of data with a space inbetween. |
| 85 |
|
|
print "$name $value\n"; |
| 86 |
|
|
} |
| 87 |
|
|
|
| 88 |
|
|
|
| 89 |
|
|
# sub to find out the list of all usernames logged |
| 90 |
|
|
# in to the machine and how many there are. |
| 91 |
|
|
sub include_users() { |
| 92 |
|
|
|
| 93 |
|
|
# Find out all users on this machine. |
| 94 |
|
|
my($users) = `$usersbin`; |
| 95 |
|
|
$users = "\n" unless defined $users; |
| 96 |
|
|
chomp $users; |
| 97 |
|
|
my($users_count) = 0; |
| 98 |
|
|
$users_count++ while $users =~ /\w+/g; |
| 99 |
|
|
my($users_list) = $users." "; |
| 100 |
|
|
|
| 101 |
|
|
&print_pair(0, "packet.users.count", $users_count); |
| 102 |
|
|
&print_pair("unknown", "packet.users.list", $users_list); |
| 103 |
|
|
} |