-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkgraph.pl
executable file
·142 lines (118 loc) · 5.23 KB
/
mkgraph.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/perl
# statgraph: A simple host resource graphing tool
# Copyright (C) 2004-2011 Ben Charlton <ben@spod.cx>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA, or see http://www.gnu.org
use strict;
use StatGraph;
use File::Copy;
## TODO: configure with getopt
my $config = "statgraph.conf";
## Get configuration
open CONFIG, $config || die "Cannot open $config: $!";
my @CONFIG = (<CONFIG>);
close CONFIG;
my %CONFIG = confparse(\@CONFIG);
my %HOSTS = %{$CONFIG{hosts}};
%CONFIG = %{$CONFIG{config}};
## Set default configuration options if they've not been specified
my $defaultport = $CONFIG{defaultport} || 27001;
my $offsets = $CONFIG{offsets} || '10800 86400 604800 2419200 31536000';
my @offsets = split(/\s+/, $offsets);
my $rrdlocation = $CONFIG{rrdlocation} || "rrd/";
my $graphlocation = $CONFIG{graphlocation} || "graphs/";
open INDEX, ">$graphlocation/index.html.tmp";
print INDEX htmlheader("StatGraph results");
print INDEX "<h1>StatGraph results</h1>";
print INDEX "<p>Last updated: " . nice_date;
print INDEX "<ul>";
## Main loop - run once for each host
foreach my $host (sort keys %HOSTS) {
print "+ $HOSTS{$host}{displayname}\n";
print INDEX "<li><a href='$host.html'>$host</a>";
print INDEX " - $HOSTS{$host}{comment}</li>";
my %ignore;
foreach (split(/\s+/, $HOSTS{$host}{ignore})) {
$ignore{$_} = 1;
}
open CACHE, "$rrdlocation$host.txt" || warn "$host has no cache\n";
my @res = (<CACHE>);
close CACHE;
my %results = sgparse(\@res);
## Simple check that we've got reasonable statgrab data
unless ($results{const}{0} eq '0') {
warn "Bad statgrab results for $host";
next;
}
open SUMM, ">$graphlocation$host.html";
print SUMM htmlheader("$host summary");
my %colours = %{$CONFIG{colour}};
print SUMM "<h1>$host summary for last " . nice_time($offsets[0]). "</h1>";
print SUMM "<p>$HOSTS{$host}{comment}</p>";
print SUMM "<p>Last updated: " . nice_date;
my %nicenames = (
'cpu' => "CPU utilisation for $host",
'load' => "Load averages for $host",
'mem' => "Memory usage for $host",
'page' => "Paging activity for $host",
'proc' => "Processes for $host",
'user' => "User activity for $host",
'swap' => "Swap usage for $host");
## Generate graphs
foreach ('cpu', 'load', 'mem', 'page', 'proc', 'user', 'swap') {
if (-e "$rrdlocation$host.$_.rrd") {
create_graph($rrdlocation, $graphlocation, $_, $host, '', \@offsets, '', \%colours);
create_page($graphlocation, $_, $host, '', \@offsets);
print SUMM "<h2>$nicenames{$_}</h2><a href='$host-$_.html'><img src='$host.$_.$offsets[0].png'></a><br />\n";
}
}
## net device RRDs
foreach my $dev (sort keys %{ $results{net} }) {
unless (defined $ignore{"net.$dev"}) {
if (-e "$rrdlocation$host.net.$dev.rrd") {
create_graph($rrdlocation, $graphlocation, 'net', $host, $dev, \@offsets, '', \%colours);
create_page($graphlocation, 'net', $host, $dev, \@offsets);
print SUMM "<h2>Network IO for $host on $dev</h2><a href='$host-net.$dev.html'><img src='$host.net.$dev.$offsets[0].png'></a><br />\n";
}
}
}
## disk device RRDs
foreach my $dev (sort keys %{ $results{disk} }) {
unless (defined $ignore{"disk.$dev"}) {
if (-e "$rrdlocation$host.disk.$dev.rrd") {
create_graph($rrdlocation, $graphlocation, 'disk', $host, $dev, \@offsets, '', \%colours);
create_page($graphlocation, 'disk', $host, $dev, \@offsets);
print SUMM "<h2>Disk IO for $host on $dev</h2><a href='$host-disk.$dev.html'><img src='$host.disk.$dev.$offsets[0].png'></a><br />\n";
}
}
}
## fs device RRDs
foreach my $dev (sort keys %{ $results{fs} }) {
unless (defined $ignore{"fs.$dev"}) {
if (-e "$rrdlocation$host.fs.$dev.rrd") {
create_graph($rrdlocation, $graphlocation, 'fs', $host, $dev, \@offsets, $results{fs}{$dev}{mnt_point}, \%colours);
create_page($graphlocation, 'fs', $host, $dev, \@offsets);
print SUMM "<h2>Filesystem Utilisation for $host on $dev</h2><a href='$host-fs.$dev.html'><img src='$host.fs.$dev.$offsets[0].png'></a><br />\n";
}
}
}
print SUMM htmlfooter;
close SUMM;
}
print INDEX "</ul>";
print INDEX htmlfooter;
close INDEX;
move("$graphlocation/index.html.tmp", "$graphlocation/index.html");