ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/plugins/perl/i-scream_users.pl
Revision: 1.2
Committed: Tue Dec 18 04:07:17 2001 UTC (24 years ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.1: +6 -12 lines
Log Message:
Solaris process accounting made me notice the number of times `uname` was
being called. It was way over the top, especially as it's unlikely to
change during an invocation of ihost :) Before it was run by each plugin
every time they were run - in total 50 times a minute :/ Now, it's run once
by ihost at startup, and that's it. The value read at startup is passed as
a command line argument to each plugin. The plugins can fallback on working
out the ostype using `uname` if they don't get given one on the command
line.

File Contents

# Content
1 #!/usr/bin/perl -w
2
3 #-----------------------------------------------------------------
4 # i-scream host plugin - user information collection
5 # $Author: tdb $
6 # $Id: i-scream_users.pl,v 1.1 2001/11/19 21:42:11 tdb 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 # Get the OS type from the args, or try towork it out
19 my($ostype) = $ARGV[0];
20 $ostype = `uname -s` if not defined $ostype;
21 chomp($ostype);
22
23 # Decide which paths we should use.
24 my($usersbin);
25 if ($ostype eq "SunOS") {
26 # covers: Solaris 7/8
27 $usersbin = "/usr/ucb/users";
28 }
29 elsif ($ostype eq "Linux" || $ostype eq "FreeBSD") {
30 # covers: Debian r2.2
31 # covers: FreeBSD 4.X
32 $usersbin = "/usr/bin/users";
33 }
34 else {
35 print "i-scream_users.pl Error: Unable to identify system type - \"$ostype\".\n";
36 print "\"uname -s\" does not report one of the following known types;\n";
37 print " SunOS, Linux, FreeBSD\n";
38 exit(1);
39 }
40
41 # Run the following components: -
42 &include_users();
43
44 # End the program normally.
45 exit(0);
46
47
48
49
50 # sub to print pairs of data, separated by a single space character.
51 # If the second argument is undefined, then the pair is still printed,
52 # however, the value shall be displayed as the the 'default' value
53 # if the passed value was undefined.
54 sub print_pair($$$) {
55 my($default, $name, $value) = @_;
56
57 if (!defined $value) {
58 $value = $default;
59 }
60
61 # Remove the trailing linefeed if we've not already done so.
62 chomp($value);
63
64 # print the pair of data with a space inbetween.
65 print "$name $value\n";
66 }
67
68
69 # sub to find out the list of all usernames logged
70 # in to the machine and how many there are.
71 sub include_users() {
72
73 # Find out all users on this machine.
74 my($users) = `$usersbin`;
75 $users = "\n" unless defined $users;
76 chomp $users;
77 my($users_count) = 0;
78 $users_count++ while $users =~ /\w+/g;
79 my($users_list) = $users." ";
80
81 &print_pair(0, "packet.users.count", $users_count);
82 &print_pair("unknown", "packet.users.list", $users_list);
83 }