1 |
#!/usr/bin/perl -w |
2 |
# i-scream libstatgrab |
3 |
# http://www.i-scream.org |
4 |
# Copyright (C) 2000-2004 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 |
# $Id: statgrab-make-mrtg-config.in,v 1.5 2004/04/06 14:53:00 tdb Exp $ |
21 |
|
22 |
use strict; |
23 |
use Getopt::Long; |
24 |
|
25 |
my $progname = "statgrab-make-mrtg-config"; |
26 |
my $statgrab = "statgrab"; |
27 |
my $workdir = undef; |
28 |
|
29 |
my $kib = 1024; |
30 |
my $mib = $kib * $kib; |
31 |
|
32 |
# Type 0 is plain integers. |
33 |
my $KIBIBYTES = 1; |
34 |
my $PERCENT = 2; |
35 |
my $FLOAT = 3; |
36 |
|
37 |
# Print an entry in the MRTG config file. |
38 |
sub entry ($$$$$$$$$$) { |
39 |
my ($title, $vali, $valo, $max, $ylegend, $yunit, $legendi, $legendo, $gauge, $type) = @_; |
40 |
my $name = $vali; |
41 |
my $options = ""; |
42 |
$options .= " noo" unless defined $valo; |
43 |
$options .= " gauge" if $gauge; |
44 |
my $sgoptions = ""; |
45 |
$sgoptions .= " -o -p" if $type == $PERCENT; |
46 |
$sgoptions .= " -f 1000" if $type == $FLOAT; |
47 |
|
48 |
print "\n"; |
49 |
print "Title[$name]: $title\n"; |
50 |
print "PageTop[$name]: $title\n"; |
51 |
print "MaxBytes[$name]: $max\n"; |
52 |
print "YLegend[$name]: $ylegend\n"; |
53 |
print "ShortLegend[$name]: $yunit\n"; |
54 |
print "LegendI[$name]: $legendi\n"; |
55 |
print "LegendO[$name]: $legendo\n" if defined $valo; |
56 |
if ($type == $KIBIBYTES) { |
57 |
print "kMG[$name]: Ki,Mi,Gi,Ti\n"; |
58 |
$sgoptions .= " -K"; |
59 |
} |
60 |
$valo = "const.0" unless defined $valo; |
61 |
print "Options[$name]:$options\n" if $options ne ""; |
62 |
print "Target[$name]: `$statgrab$sgoptions -m $vali $valo`\n"; |
63 |
} |
64 |
|
65 |
my $package_version = '@PACKAGE_VERSION@'; |
66 |
my $package_bugreport = '@PACKAGE_BUGREPORT@'; |
67 |
my $help_text = <<EOF; |
68 |
Usage: $progname [OPTION]... |
69 |
Generate MRTG configuration from statgrab output and write it to stdout. |
70 |
|
71 |
--no-header Don't print MRTG global options; useful if you |
72 |
want to include the output of this script in |
73 |
another MRTG config file |
74 |
--workdir PATH Use PATH for MRTG's WorkDir option |
75 |
--statgrab PATH Specify location of statgrab binary |
76 |
(default "statgrab") |
77 |
--help Display this help and exit |
78 |
|
79 |
Version $package_version - report bugs to $package_bugreport. |
80 |
EOF |
81 |
|
82 |
sub fatal ($) { |
83 |
my ($message) = @_; |
84 |
die "$progname: $message\n"; |
85 |
} |
86 |
|
87 |
sub main () { |
88 |
GetOptions('statgrab=s' => \$statgrab, |
89 |
'workdir=s' => \$workdir, |
90 |
'no-header' => \my $no_header, |
91 |
'help' => \my $help) or die $help_text; |
92 |
if ($help) { |
93 |
print "$help_text"; |
94 |
exit 0; |
95 |
} |
96 |
|
97 |
unless ($no_header or defined $workdir) { |
98 |
fatal "must specify --workdir or --no-header" |
99 |
} |
100 |
|
101 |
my %stats = (); |
102 |
my %toplevel = (); |
103 |
my %disks = (); |
104 |
my %fss = (); |
105 |
my %nets = (); |
106 |
open STATGRAB, "$statgrab|" or fatal "can't run statgrab"; |
107 |
while (<STATGRAB>) { |
108 |
chomp; |
109 |
/^([^=]*) = (.*)$/ or fatal "bad line in statgrab output"; |
110 |
$stats{$1} = $2; |
111 |
|
112 |
my @parts = split /\./, $1; |
113 |
$toplevel{$parts[0]} = 1; |
114 |
$disks{$parts[1]} = 1 if $parts[0] eq "disk"; |
115 |
$fss{$parts[1]} = 1 if $parts[0] eq "fs"; |
116 |
$nets{$parts[1]} = 1 if $parts[0] eq "net"; |
117 |
} |
118 |
close STATGRAB; |
119 |
|
120 |
unless ($no_header) { |
121 |
print "WorkDir: $workdir\n"; |
122 |
print "Options[^]: growright\n"; |
123 |
print "WriteExpires: Yes\n"; |
124 |
} |
125 |
|
126 |
if (exists $toplevel{"cpu"}) { |
127 |
entry("CPU idle", "cpu.idle", undef, "100", "Idle", "%", "idle", undef, 1, $PERCENT); |
128 |
entry("CPU iowait", "cpu.iowait", undef, "100", "iowait", "%", "iowait", undef, 1, $PERCENT); |
129 |
entry("CPU kernel", "cpu.kernel", undef, "100", "Kernel", "%", "kernel", undef, 1, $PERCENT); |
130 |
entry("CPU nice", "cpu.nice", undef, "100", "Nice", "%", "nice", undef, 1, $PERCENT); |
131 |
entry("CPU swap", "cpu.swap", undef, "100", "Swap", "%", "swap", undef, 1, $PERCENT); |
132 |
entry("CPU user", "cpu.user", undef, "100", "User", "%", "user", undef, 1, $PERCENT); |
133 |
} |
134 |
|
135 |
foreach my $disk (sort keys %disks) { |
136 |
my $name = $stats{"disk.$disk.disk_name"}; |
137 |
entry("Disk $name IO", "disk.$disk.read_bytes", "disk.$disk.write_bytes", 100*$mib, "IO rate", "KiB/s", "read", "write", 0, $KIBIBYTES); |
138 |
} |
139 |
|
140 |
foreach my $fs (sort keys %fss) { |
141 |
my $name = $stats{"fs.$fs.mnt_point"}; |
142 |
my $size = $stats{"fs.$fs.size"}; |
143 |
my $inodes = $stats{"fs.$fs.total_inodes"}; |
144 |
entry("Filesystem $name space usage", "fs.$fs.used", undef, $size, "Space used", "KiB", "used", undef, 1, $KIBIBYTES); |
145 |
entry("Filesystem $name inode usage", "fs.$fs.used_inodes", undef, $inodes, "Inodes used", "inodes", "used", undef, 1, 0); |
146 |
} |
147 |
|
148 |
if (exists $toplevel{"load"}) { |
149 |
entry("Load average over 1 minute", "load.min1", undef, 100, "Load average", "running * 1000", "load", undef, 1, $FLOAT); |
150 |
entry("Load average over 5 minutes", "load.min5", undef, 100, "Load average", "running * 1000", "load", undef, 1, $FLOAT); |
151 |
entry("Load average over 15 minutes", "load.min15", undef, 100, "Load average", "running * 1000", "load", undef, 1, $FLOAT); |
152 |
} |
153 |
|
154 |
if (exists $toplevel{"mem"}) { |
155 |
my $total = $stats{"mem.total"}; |
156 |
entry("Memory usage", "mem.used", "mem.cache", $total, "Memory usage", "KiB", "total", "cache", 1, $KIBIBYTES); |
157 |
} |
158 |
|
159 |
foreach my $net (sort keys %nets) { |
160 |
my $name = $stats{"net.$net.interface_name"}; |
161 |
# FIXME should be able to discover interface speed |
162 |
entry("Network interface $name IO", "net.$net.rx", "net.$net.tx", 100*$mib, "Network IO", "KiB/s", "rx", "tx", 0, $KIBIBYTES); |
163 |
} |
164 |
|
165 |
if (exists $toplevel{"page"}) { |
166 |
# FIXME what's a sensible maximum? |
167 |
entry("Paging IO", "page.in", "page.out", 1000, "Paging IO", "pages", "in", "out", 0, 0); |
168 |
} |
169 |
|
170 |
if (exists $toplevel{"proc"}) { |
171 |
# FIXME mildly silly assumption |
172 |
my $maxproc = 65536; |
173 |
entry("Processes running", "proc.running", undef, $maxproc, "Running", "procs", "running", undef, 1, 0); |
174 |
entry("Processes sleeping", "proc.sleeping", undef, $maxproc, "Sleeping", "procs", "running", undef, 1, 0); |
175 |
entry("Processes stopped", "proc.stopped", undef, $maxproc, "Stopped", "procs", "running", undef, 1, 0); |
176 |
entry("Processes", "proc.total", undef, $maxproc, "Total", "procs", "running", undef, 1, 0); |
177 |
entry("Processes zombie", "proc.zombie", undef, $maxproc, "Zombie", "procs", "running", undef, 1, 0); |
178 |
} |
179 |
|
180 |
if (exists $toplevel{"swap"}) { |
181 |
my $swapsize = $stats{"swap.total"}; |
182 |
entry("Swap usage", "swap.used", undef, $swapsize, "Swap usage", "KiB", "used", undef, 1, $KIBIBYTES); |
183 |
} |
184 |
|
185 |
if (exists $toplevel{"user"}) { |
186 |
entry("Users", "user.num", undef, 1000, "Users", "users", "users", undef, 1, 0); |
187 |
} |
188 |
} |
189 |
|
190 |
main(); |