ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/statgrab/statgrab-make-mrtg-config.in
Revision: 1.1
Committed: Thu Aug 28 11:26:42 2003 UTC (21 years, 4 months ago) by ats
Branch: MAIN
Log Message:
Add scripts to generate MRTG config and index page from statgrab output.

File Contents

# Content
1 #!/usr/bin/perl -w
2 # i-scream central monitoring system
3 # http://www.i-scream.org
4 # Copyright (C) 2000-2003 i-scream
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 use strict;
21 use Getopt::Long;
22
23 my $progname = "statgrab-make-mrtg-config";
24 my $statgrab = "statgrab";
25 my $workdir = undef;
26
27 my $kib = 1024;
28 my $mib = $kib * $kib;
29
30 # Print an entry in the MRTG config file.
31 sub entry ($$$$$$$$$$) {
32 my ($title, $vali, $valo, $max, $ylegend, $yunit, $legendi, $legendo, $gauge, $in_kib) = @_;
33 my $name = $vali;
34 my $options = "";
35 $options .= " noo" unless defined $valo;
36 $options .= " gauge" if $gauge;
37
38 print "\n";
39 print "Title[$name]: $title\n";
40 print "PageTop[$name]: $title\n";
41 print "MaxBytes[$name]: $max\n";
42 print "YLegend[$name]: $ylegend\n";
43 print "ShortLegend[$name]: $yunit\n";
44 print "LegendI[$name]: $legendi\n";
45 print "LegendO[$name]: $legendo\n" if defined $valo;
46 if ($in_kib) {
47 print "kMG[$name]: Ki,Mi,Gi,Ti\n";
48 }
49 $valo = "const.0" unless defined $valo;
50 print "Options[$name]:$options\n" if $options ne "";
51 print "Target[$name]: `$statgrab -p -m $vali $valo`\n";
52 }
53
54 my $package_version = '@PACKAGE_VERSION@';
55 my $package_bugreport = '@PACKAGE_BUGREPORT@';
56 my $help_text = <<EOF;
57 Usage: $progname [OPTION]...
58 Generate MRTG configuration from statgrab output and write it to stdout.
59
60 --no-header Don't print MRTG global options; useful if you
61 want to include the output of this script in
62 another MRTG config file
63 --workdir PATH Use PATH for MRTG's WorkDir option
64 --statgrab PATH Specify location of statgrab binary
65 (default "statgrab")
66 --help Display this help and exit
67
68 Version $package_version - report bugs to $package_bugreport.
69 EOF
70
71 sub fatal ($) {
72 my ($message) = @_;
73 die "$progname: $message\n";
74 }
75
76 sub main () {
77 GetOptions('statgrab=s' => \$statgrab,
78 'workdir=s' => \$workdir,
79 'no-header' => \my $no_header,
80 'help' => \my $help) or die $help_text;
81 if ($help) {
82 print "$help_text";
83 exit 0;
84 }
85
86 unless ($no_header or defined $workdir) {
87 fatal "must specify --workdir or --no-header"
88 }
89
90 my %stats = ();
91 my %toplevel = ();
92 my %disks = ();
93 my %fss = ();
94 my %nets = ();
95 open STATGRAB, "$statgrab|" or fatal "can't run statgrab";
96 while (<STATGRAB>) {
97 chomp;
98 /^([^=]*) = (.*)$/ or fatal "bad line in statgrab output";
99 $stats{$1} = $2;
100
101 my @parts = split /\./, $1;
102 $toplevel{$parts[0]} = 1;
103 $disks{$parts[1]} = 1 if $parts[0] eq "disk";
104 $fss{$parts[1]} = 1 if $parts[0] eq "fs";
105 $nets{$parts[1]} = 1 if $parts[0] eq "net";
106 }
107 close STATGRAB;
108
109 unless ($no_header) {
110 print "WorkDir: $workdir\n";
111 print "Options[^]: growright\n";
112 print "WriteExpires: Yes\n";
113 }
114
115 if (exists $toplevel{"cpu"}) {
116 entry("CPU idle", "cpu.idle", undef, "100", "Idle", "%", "idle", undef, 1, 0);
117 entry("CPU iowait", "cpu.iowait", undef, "100", "iowait", "%", "iowait", undef, 1, 0);
118 entry("CPU kernel", "cpu.kernel", undef, "100", "Kernel", "%", "kernel", undef, 1, 0);
119 entry("CPU nice", "cpu.nice", undef, "100", "Nice", "%", "nice", undef, 1, 0);
120 entry("CPU swap", "cpu.swap", undef, "100", "Swap", "%", "swap", undef, 1, 0);
121 entry("CPU user", "cpu.user", undef, "100", "User", "%", "user", undef, 1, 0);
122 }
123
124 foreach my $disk (sort keys %disks) {
125 my $name = $stats{"disk.$disk.disk_name"};
126 entry("Disk $name IO", "disk.$disk.read_bytes", "disk.$disk.write_bytes", 100*$mib, "IO rate", "B/s", "read", "write", 0, 0);
127 }
128
129 foreach my $fs (sort keys %fss) {
130 my $name = $stats{"fs.$fs.mnt_point"};
131 my $size = $stats{"fs.$fs.size"};
132 my $inodes = $stats{"fs.$fs.total_inodes"};
133 entry("Filesystem $name space usage", "fs.$fs.used", undef, $size, "Space used", "B", "used", undef, 1, 0);
134 entry("Filesystem $name inode usage", "fs.$fs.used_inodes", undef, $inodes, "Inodes used", "inodes", "used", undef, 1, 0);
135 }
136
137 if (exists $toplevel{"load"}) {
138 entry("Load average over 1 minute", "load.min1", undef, 100, "Load average", "running", "load", undef, 1, 0);
139 entry("Load average over 5 minutes", "load.min5", undef, 100, "Load average", "running", "load", undef, 1, 0);
140 entry("Load average over 15 minutes", "load.min15", undef, 100, "Load average", "running", "load", undef, 1, 0);
141 }
142
143 if (exists $toplevel{"mem"}) {
144 my $total = $stats{"mem.total"};
145 entry("Memory usage", "mem.used", "mem.cache", $total, "Memory usage", "B", "total", "cache", 1, 0);
146 }
147
148 foreach my $net (sort keys %nets) {
149 my $name = $stats{"net.$net.interface_name"};
150 # FIXME should be able to discover interface speed
151 entry("Network interface $name IO", "net.$net.rx", "net.$net.tx", 100*$mib, "Network IO", "B", "rx", "tx", 0, 0);
152 }
153
154 if (exists $toplevel{"page"}) {
155 # FIXME what's a sensible maximum?
156 entry("Paging IO", "page.in", "page.out", 1000, "Paging IO", "pages", "in", "out", 0, 0);
157 }
158
159 if (exists $toplevel{"proc"}) {
160 # FIXME mildly silly assumption
161 my $maxproc = 65536;
162 entry("Processes running", "proc.running", undef, $maxproc, "Running", "procs", "running", undef, 1, 0);
163 entry("Processes sleeping", "proc.sleeping", undef, $maxproc, "Sleeping", "procs", "running", undef, 1, 0);
164 entry("Processes stopped", "proc.stopped", undef, $maxproc, "Stopped", "procs", "running", undef, 1, 0);
165 entry("Processes", "proc.total", undef, $maxproc, "Total", "procs", "running", undef, 1, 0);
166 entry("Processes zombie", "proc.zombie", undef, $maxproc, "Zombie", "procs", "running", undef, 1, 0);
167 }
168
169 if (exists $toplevel{"swap"}) {
170 my $swapsize = $stats{"swap.total"};
171 entry("Swap usage", "swap.used", undef, $swapsize, "Swap usage", "B", "used", undef, 1, 0);
172 }
173
174 if (exists $toplevel{"user"}) {
175 entry("Users", "user.num", undef, 1000, "Users", "users", "users", undef, 1, 0);
176 }
177 }
178
179 main();