4 # (c)2000 Cliff Woolley, Washington and Lee University
7 # A UNIX counterpart to Jim Drews' MRTG Extension for netware servers
8 # Mimics output of mrtgext.nlm using output of various standard UNIX
9 # programs (df, uptime, and uname)
11 # Dependencies: I make some assumptions about the output format of
12 # your df and uptime commands. If you have nonstandard outputs for
13 # any of these, either pick a different command that gives more
14 # standard output or modify the script below. Example: use /usr/bin/bdf
15 # on HP-UX instead of /usr/bin/df, because bdf follows the output format
16 # I expect while df does not. This was written on Linux and tested on
17 # HP-UX 10.20 (with changes to the subroutines at the bottom of the
18 # program to reflect HP's command parameters); similar tweaking could
19 # well be required to port this to other platforms. If you get it
20 # working on your platform, please send me any changes you had to
21 # make so I can try to incorporate them.
24 # Following is what I expect the programs' outputs to look like:
27 # Filesystem 1k-blocks Used Available Use% Mounted on
28 # /dev/sda1 1014696 352708 609612 37% /
29 # /dev/sda2 2262544 586712 1559048 27% /apps
30 # /dev/sda3 4062912 566544 3286604 15% /share
31 # /dev/sr0 651758 651758 0 100% /cdrom
35 # 3:17pm up 15 days, 4:40, 5 users, load average: 0.12, 0.26, 0.33
39 ###############################################################
40 # Configuration section
41 ###############################################################
43 $dfcmd = "/bin/df 2>/dev/null";
44 $uptimecmd = "/usr/bin/uptime";
45 %customcmds = ( "PROCS" => "numprocesses",
46 "ZOMBIES" => "numzombies",
47 "MEMFREE" => "memfree",
48 "SWAPUSED" => "swapused",
49 "TCPCONNS" => "tcpconns",
50 "CLIENTS" => "ipclients" );
51 # These are functions that you can
52 # define and customize for your system.
53 # You probably need to change the provided
54 # subroutines to work on your system (if
57 $rootfsnickname = "root"; # this is necessary as a kludge to
58 # better match the netware behavior.
59 # if you already have a _filesystem_
60 # mounted as /root, then you'll need
61 # to change this to something else
66 ###############################################################
68 ###############################################################
75 $DEBUG = $ARGV[0] unless ($DEBUG);
76 $SIG{'ALRM'} = sub { exit 1; };
78 # some things never change
84 print scalar localtime,": mrtgext.pl started\n";
92 $items =~ s/[\r\n]//g;
93 ( $DEBUG ) && print scalar localtime,": request: \"$items\"\n";
94 my @items = split (/\s+/,"$items");
95 ( $DEBUG ) && print scalar localtime,": ",scalar @items," item(s) to process\n";
97 my $uptime = `$uptimecmd`;
98 my @df = grep {/^\//} `$dfcmd`;
102 foreach $_ (@items) {
103 ( $DEBUG ) && print scalar localtime,": processing item #$processed: \"$_\"\n";
104 $_ = uc; #convert $_ to upper case
106 $uptime =~ /load average: ([^,]+),/;
109 elsif ( /^UTIL5$/ ) {
110 $uptime =~ /load average: [^,]+, ([^,]+)/;
113 elsif ( /^UTIL15$/ ) {
114 $uptime =~ /load average: [^,]+, [^,]+, ([^,]+)/;
117 elsif ( /^CONNECT$/ ) {
118 $uptime =~ /(\d+) users?,/;
124 elsif ( /^UPTIME$/ ) {
125 $uptime =~ /up (.*),\s+\d+\s+users?,/;
128 elsif ( /^VOLUMES$/ ) {
129 foreach $dfline (@df) {
130 my $volname = (split(/\s+/, "$dfline"))[5];
131 $volname =~ s/^\/$/$rootfsnickname/;
133 $volname =~ s/\//_
/g
;
137 elsif ( /^VF(\w*)$/ ) {
138 my $volname = ("$1" eq uc("$rootfsnickname")) ?
"/" : "$1";
139 foreach $dfline (@df) {
140 my @dfline = split(/\s+/, "$dfline");
141 if ($dfline[5] =~ /^\/?
$volname$/i
) {
142 print (($dfline[1]-$dfline[2]) * 1024,"\n");
146 ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
149 elsif ( /^VU(\w*)$/ ) {
150 my $volname = ("$1" eq uc("$rootfsnickname")) ?
"/" : "$1";
151 foreach $dfline (@df) {
152 my @dfline = split(/\s+/, "$dfline");
153 if ($dfline[5] =~ /^\/?
$volname$/i
) {
154 print ($dfline[2] * 1024,"\n");
158 ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
161 elsif ( /^VS(\w*)$/ ) {
162 my $volname = ("$1" eq uc("$rootfsnickname")) ?
"/" : "$1";
163 foreach $dfline (@df) {
164 my @dfline = split(/\s+/, "$dfline");
165 if ($dfline[5] =~ /^\/?
$volname$/i
) {
166 print ($dfline[1] * 1024,"\n");
170 ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
173 elsif ( /^VKF(\w*)$/ ) {
174 my $volname = ("$1" eq uc("$rootfsnickname")) ?
"/" : "$1";
175 foreach $dfline (@df) {
176 my @dfline = split(/\s+/, "$dfline");
177 if ($dfline[5] =~ /^\/?
$volname$/i
) {
178 print (($dfline[1]-$dfline[2]),"\n");
182 ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
185 elsif ( /^VKU(\w*)$/ ) {
186 my $volname = ("$1" eq uc("$rootfsnickname")) ?
"/" : "$1";
187 foreach $dfline (@df) {
188 my @dfline = split(/\s+/, "$dfline");
189 if ($dfline[5] =~ /^\/?
$volname$/i
) {
190 print ($dfline[2],"\n");
194 ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
197 elsif ( /^VKS(\w*)$/ ) {
198 my $volname = ("$1" eq uc("$rootfsnickname")) ?
"/" : "$1";
199 foreach $dfline (@df) {
200 my @dfline = split(/\s+/, "$dfline");
201 if ($dfline[5] =~ /^\/?
$volname$/i
) {
202 print ($dfline[1],"\n");
206 ( $DEBUG ) && print scalar localtime,": ERROR: volume not found.\n";
212 elsif (exists( $customcmds{"$_"} )) {
213 my $cmdsub = "$customcmds{$_}";
221 ( $DEBUG ) && print scalar localtime,": done.\n";
224 ###############################################################
225 # CUSTOMIZED PROCEDURES
226 ###############################################################
230 my $num = `/bin/ps -eaf | /usr/bin/tail -n +2 | /usr/bin/wc -l`;
239 my $num = `/bin/ps -afx | /usr/bin/awk '{print \$3}' | /usr/bin/grep Z | /usr/bin/tail -n +2 | /usr/bin/wc -l`;
248 my $num = `/bin/netstat -nt | /usr/bin/tail -n +3 | /usr/bin/wc -l`;
257 my $num = `/bin/netstat -nt | /usr/bin/tail -n +3 | /usr/bin/awk '{print \$5}' | /bin/cut -d : -f 1 | /usr/bin/sort -nu | /usr/bin/wc -l`;
266 open( FP
, "/proc/meminfo" );
270 # total: used: free: shared: buffers: cached:
271 # Mem: 994615296 592801792 401813504 91193344 423313408 93118464
272 # Swap: 204791808 0 204791808
273 my ($total,$free,$buffers,$cache) = (split(/ +/,$meminfo[1]))[1,3,5,6];
275 int(($free+$buffers+$cache)/$total*100);
280 open( FP
, "/proc/meminfo" );
284 # total: used: free: shared: buffers: cached:
285 # Mem: 994615296 592424960 402190336 89821184 423313408 93077504
286 # Swap: 204791808 0 204791808
288 my ($total,$used) = (split(/ +/,$meminfo[2]))[1,2];
290 int($used/$total*100);