mirror of https://github.com/mikaku/Monitorix.git
3.0: added support for 'user' graph
This commit is contained in:
parent
2156f9e405
commit
05c87b4b2b
|
@ -0,0 +1,460 @@
|
|||
#
|
||||
# Monitorix - A lightweight system monitoring tool.
|
||||
#
|
||||
# Copyright (C) 2005-2012 by Jordi Sanfeliu <jordi@fibranet.cat>
|
||||
#
|
||||
# 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; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
|
||||
package user;
|
||||
|
||||
#use strict;
|
||||
use warnings;
|
||||
use Monitorix;
|
||||
use RRDs;
|
||||
use Exporter 'import';
|
||||
our @EXPORT = qw(user_init user_update user_cgi);
|
||||
|
||||
sub user_init {
|
||||
my $myself = (caller(0))[3];
|
||||
my ($package, $config, $debug) = @_;
|
||||
my $rrd = $config->{base_lib} . $package . ".rrd";
|
||||
|
||||
if(!(-e $rrd)) {
|
||||
logger("Creating '$rrd' file.");
|
||||
eval {
|
||||
RRDs::create($rrd,
|
||||
"--step=60",
|
||||
"DS:user_sys:GAUGE:120:0:U",
|
||||
"DS:user_smb:GAUGE:120:0:U",
|
||||
"DS:user_mac:GAUGE:120:0:U",
|
||||
"DS:user_val1:GAUGE:120:0:U",
|
||||
"DS:user_val2:GAUGE:120:0:U",
|
||||
"DS:user_val3:GAUGE:120:0:U",
|
||||
"DS:user_val4:GAUGE:120:0:U",
|
||||
"DS:user_val5:GAUGE:120:0:U",
|
||||
"RRA:AVERAGE:0.5:1:1440",
|
||||
"RRA:AVERAGE:0.5:30:336",
|
||||
"RRA:AVERAGE:0.5:60:744",
|
||||
"RRA:AVERAGE:0.5:1440:365",
|
||||
"RRA:MIN:0.5:1:1440",
|
||||
"RRA:MIN:0.5:30:336",
|
||||
"RRA:MIN:0.5:60:744",
|
||||
"RRA:MIN:0.5:1440:365",
|
||||
"RRA:MAX:0.5:1:1440",
|
||||
"RRA:MAX:0.5:30:336",
|
||||
"RRA:MAX:0.5:60:744",
|
||||
"RRA:MAX:0.5:1440:365",
|
||||
"RRA:LAST:0.5:1:1440",
|
||||
"RRA:LAST:0.5:30:336",
|
||||
"RRA:LAST:0.5:60:744",
|
||||
"RRA:LAST:0.5:1440:365",
|
||||
);
|
||||
};
|
||||
my $err = RRDs::error;
|
||||
if($@ || $err) {
|
||||
logger("$@") unless !$@;
|
||||
if($err) {
|
||||
logger("ERROR: while creating $rrd: $err");
|
||||
if($err eq "RRDs::error") {
|
||||
logger("... is the RRDtool Perl package installed?");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
push(@{$config->{func_update}}, $package);
|
||||
logger("$myself: Ok") if $debug;
|
||||
}
|
||||
|
||||
sub user_update {
|
||||
my $myself = (caller(0))[3];
|
||||
my ($package, $config, $debug) = @_;
|
||||
my $rrd = $config->{base_lib} . $package . ".rrd";
|
||||
|
||||
my $sys;
|
||||
my $smb;
|
||||
my $mac;
|
||||
|
||||
my @data;
|
||||
my $rrdata = "N";
|
||||
|
||||
open(IN, "who -q |");
|
||||
while(<IN>) {
|
||||
if(/^#\s+\S+=(\d+)/) {
|
||||
$sys = $1;
|
||||
last;
|
||||
}
|
||||
}
|
||||
close(IN);
|
||||
|
||||
$smb = 0;
|
||||
open(IN, "smbstatus -b 2>/dev/null |");
|
||||
while(<IN>) {
|
||||
if(/^----------/) {
|
||||
$smb++;
|
||||
next;
|
||||
}
|
||||
if($smb) {
|
||||
$smb++ unless !$_;
|
||||
}
|
||||
}
|
||||
close(IN);
|
||||
$smb--;
|
||||
|
||||
open(IN, "macusers 2>/dev/null |");
|
||||
@data = <IN>;
|
||||
close(IN);
|
||||
$mac = scalar(@data) - 1;
|
||||
$mac = 0 unless @data;
|
||||
|
||||
$rrdata .= ":$sys:$smb:$mac:0:0:0:0:0";
|
||||
RRDs::update($rrd, $rrdata);
|
||||
logger("$myself: $rrdata") if $debug;
|
||||
my $err = RRDs::error;
|
||||
logger("ERROR: while updating $rrd: $err") if $err;
|
||||
}
|
||||
|
||||
sub user_cgi {
|
||||
my ($package, $config, $cgi) = @_;
|
||||
|
||||
my $user = $config->{user};
|
||||
my @rigid = split(',', $user->{rigid});
|
||||
my @limit = split(',', $user->{limit});
|
||||
my $tf = $cgi->{tf};
|
||||
my $colors = $cgi->{colors};
|
||||
my $graph = $cgi->{graph};
|
||||
my $silent = $cgi->{silent};
|
||||
|
||||
my $u = "";
|
||||
my $width;
|
||||
my $height;
|
||||
my @riglim;
|
||||
my @tmp;
|
||||
my @tmpz;
|
||||
my $n;
|
||||
my $err;
|
||||
|
||||
my $rrd = $config->{base_lib} . $package . ".rrd";
|
||||
my $title = $config->{graph_title}->{$package};
|
||||
my $PNG_DIR = $config->{base_dir} . "/" . $config->{imgs_dir};
|
||||
|
||||
$title = !$silent ? $title : "";
|
||||
|
||||
|
||||
# text mode
|
||||
#
|
||||
if(lc($config->{iface_mode}) eq "text") {
|
||||
if($title) {
|
||||
main::graph_header($title, 2);
|
||||
print(" <tr>\n");
|
||||
print(" <td bgcolor='$colors->{title_bg_color}'>\n");
|
||||
}
|
||||
my (undef, undef, undef, $data) = RRDs::fetch("$rrd",
|
||||
"--start=-$tf->{nwhen}$tf->{twhen}",
|
||||
"AVERAGE",
|
||||
"-r $tf->{res}");
|
||||
$err = RRDs::error;
|
||||
print("ERROR: while fetching $rrd: $err\n") if $err;
|
||||
print(" <pre style='font-size: 12px; color: $colors->{fg_color}';>\n");
|
||||
print("Time Logged In Samba Netatalk\n");
|
||||
print("------------------------------------- \n");
|
||||
my $line;
|
||||
my @row;
|
||||
my $time;
|
||||
for($n = 0, $time = $tf->{tb}; $n < ($tf->{tb} * $tf->{ts}); $n++) {
|
||||
$line = @$data[$n];
|
||||
my ($sys, $smb, $mac) = @$line;
|
||||
@row = ($sys, $smb, $mac);
|
||||
$time = $time - (1 / $tf->{ts});
|
||||
printf(" %2d$tf->{tc} %6d %6d %6d\n", $time, @row);
|
||||
}
|
||||
print(" </pre>\n");
|
||||
if($title) {
|
||||
print(" </td>\n");
|
||||
print(" </tr>\n");
|
||||
main::graph_footer();
|
||||
}
|
||||
print(" <br>\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
# graph mode
|
||||
#
|
||||
if($silent eq "yes" || $silent eq "imagetag") {
|
||||
$colors->{fg_color} = "#000000"; # visible color for text mode
|
||||
$u = "_";
|
||||
}
|
||||
if($silent eq "imagetagbig") {
|
||||
$colors->{fg_color} = "#000000"; # visible color for text mode
|
||||
$u = "";
|
||||
}
|
||||
|
||||
my $PNG1 = $u . $package . "1." . $tf->{when} . ".png";
|
||||
my $PNG2 = $u . $package . "2." . $tf->{when} . ".png";
|
||||
my $PNG3 = $u . $package . "3." . $tf->{when} . ".png";
|
||||
my $PNG1z = $u . $package . "1z." . $tf->{when} . ".png";
|
||||
my $PNG2z = $u . $package . "2z." . $tf->{when} . ".png";
|
||||
my $PNG3z = $u . $package . "3z." . $tf->{when} . ".png";
|
||||
unlink ("$PNG_DIR" . "$PNG1",
|
||||
"$PNG_DIR" . "$PNG2",
|
||||
"$PNG_DIR" . "$PNG3");
|
||||
if(lc($config->{enable_zoom}) eq "y") {
|
||||
unlink ("$PNG_DIR" . "$PNG1z",
|
||||
"$PNG_DIR" . "$PNG2z",
|
||||
"$PNG_DIR" . "$PNG3z");
|
||||
}
|
||||
|
||||
if($title) {
|
||||
main::graph_header($title, 2);
|
||||
}
|
||||
if(trim($rigid[0]) eq 1) {
|
||||
push(@riglim, "--upper-limit=" . trim($limit[0]));
|
||||
} else {
|
||||
if(trim($rigid[0]) eq 2) {
|
||||
push(@riglim, "--upper-limit=" . trim($limit[0]));
|
||||
push(@riglim, "--rigid");
|
||||
}
|
||||
}
|
||||
if($title) {
|
||||
print(" <tr>\n");
|
||||
print(" <td bgcolor='$colors->{title_bg_color}'>\n");
|
||||
}
|
||||
push(@tmp, "AREA:sys#44EE44:Logged In");
|
||||
push(@tmp, "GPRINT:sys:LAST: Current\\: %3.0lf");
|
||||
push(@tmp, "GPRINT:sys:AVERAGE: Average\\: %3.0lf");
|
||||
push(@tmp, "GPRINT:sys:MIN: Min\\: %3.0lf");
|
||||
push(@tmp, "GPRINT:sys:MAX: Max\\: %3.0lf\\n");
|
||||
push(@tmp, "LINE1:sys#00EE00");
|
||||
push(@tmpz, "AREA:sys#44EE44:Logged In");
|
||||
push(@tmpz, "LINE1:sys#00EE00");
|
||||
($width, $height) = split('x', $config->{graph_size}->{main});
|
||||
if($silent =~ /imagetag/) {
|
||||
($width, $height) = split('x', $config->{graph_size}->{remote}) if $silent eq "imagetag";
|
||||
($width, $height) = split('x', $config->{graph_size}->{main}) if $silent eq "imagetagbig";
|
||||
@tmp = @tmpz;
|
||||
}
|
||||
RRDs::graph("$PNG_DIR" . "$PNG1",
|
||||
"--title=$config->{graphs}->{_user1} ($tf->{nwhen}$tf->{twhen})",
|
||||
"--start=-$tf->{nwhen}$tf->{twhen}",
|
||||
"--imgformat=PNG",
|
||||
"--vertical-label=Users",
|
||||
"--width=$width",
|
||||
"--height=$height",
|
||||
@riglim,
|
||||
"--lower-limit=0",
|
||||
@{$cgi->{version12}},
|
||||
@{$colors->{graph_colors}},
|
||||
"DEF:sys=$rrd:user_sys:AVERAGE",
|
||||
"COMMENT: \\n",
|
||||
@tmp,
|
||||
"COMMENT: \\n",
|
||||
"COMMENT: \\n",
|
||||
"COMMENT: \\n");
|
||||
$err = RRDs::error;
|
||||
print("ERROR: while graphing $PNG_DIR" . "$PNG1: $err\n") if $err;
|
||||
if(lc($config->{enable_zoom}) eq "y") {
|
||||
($width, $height) = split('x', $config->{graph_size}->{zoom});
|
||||
RRDs::graph("$PNG_DIR" . "$PNG1z",
|
||||
"--title=$config->{graphs}->{_user1} ($tf->{nwhen}$tf->{twhen})",
|
||||
"--start=-$tf->{nwhen}$tf->{twhen}",
|
||||
"--imgformat=PNG",
|
||||
"--vertical-label=Users",
|
||||
"--width=$width",
|
||||
"--height=$height",
|
||||
@riglim,
|
||||
"--lower-limit=0",
|
||||
@{$cgi->{version12}},
|
||||
@{$colors->{graph_colors}},
|
||||
"DEF:sys=$rrd:user_sys:AVERAGE",
|
||||
@tmpz);
|
||||
$err = RRDs::error;
|
||||
print("ERROR: while graphing $PNG_DIR" . "$PNG1z: $err\n") if $err;
|
||||
}
|
||||
if($title || ($silent =~ /imagetag/ && $graph =~ /user1/)) {
|
||||
if(lc($config->{enable_zoom}) eq "y") {
|
||||
if(lc($config->{disable_javascript_void}) eq "y") {
|
||||
print(" <a href=\"" . $config->{url} . $config->{imgs_dir} . $PNG1z . "\"><img src='" . $config->{url} . $config->{imgs_dir} . $PNG1 . "' border='0'></a>\n");
|
||||
}
|
||||
else {
|
||||
print(" <a href=\"javascript:void(window.open('" . $config->{url} . $config->{imgs_dir} . $PNG1z . "','','width=" . ($width + 115) . ",height=" . ($height + 100) . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . $config->{imgs_dir} . $PNG1 . "' border='0'></a>\n");
|
||||
}
|
||||
} else {
|
||||
print(" <img src='" . $config->{url} . $config->{imgs_dir} . $PNG1 . "'>\n");
|
||||
}
|
||||
}
|
||||
if($title) {
|
||||
print(" </td>\n");
|
||||
print(" <td valign='top' bgcolor='" . $colors->{title_bg_color} . "'>\n");
|
||||
}
|
||||
|
||||
undef(@riglim);
|
||||
if(trim($rigid[1]) eq 1) {
|
||||
push(@riglim, "--upper-limit=" . trim($limit[1]));
|
||||
} else {
|
||||
if(trim($rigid[1]) eq 2) {
|
||||
push(@riglim, "--upper-limit=" . trim($limit[1]));
|
||||
push(@riglim, "--rigid");
|
||||
}
|
||||
}
|
||||
undef(@tmp);
|
||||
undef(@tmpz);
|
||||
push(@tmp, "AREA:smb#EEEE44:Samba");
|
||||
push(@tmp, "GPRINT:smb:LAST: Current\\: %3.0lf\\n");
|
||||
push(@tmp, "LINE1:smb#EEEE00");
|
||||
push(@tmpz, "AREA:smb#EEEE44:Samba");
|
||||
push(@tmpz, "LINE2:smb#EEEE00");
|
||||
($width, $height) = split('x', $config->{graph_size}->{small});
|
||||
if($silent =~ /imagetag/) {
|
||||
($width, $height) = split('x', $config->{graph_size}->{remote}) if $silent eq "imagetag";
|
||||
($width, $height) = split('x', $config->{graph_size}->{main}) if $silent eq "imagetagbig";
|
||||
@tmp = @tmpz;
|
||||
push(@tmp, "COMMENT: \\n");
|
||||
push(@tmp, "COMMENT: \\n");
|
||||
push(@tmp, "COMMENT: \\n");
|
||||
}
|
||||
RRDs::graph("$PNG_DIR" . "$PNG2",
|
||||
"--title=$config->{graphs}->{_user2} ($tf->{nwhen}$tf->{twhen})",
|
||||
"--start=-$tf->{nwhen}$tf->{twhen}",
|
||||
"--imgformat=PNG",
|
||||
"--vertical-label=Users",
|
||||
"--width=$width",
|
||||
"--height=$height",
|
||||
@riglim,
|
||||
"--lower-limit=0",
|
||||
@{$cgi->{version12}},
|
||||
@{$cgi->{version12_small}},
|
||||
@{$colors->{graph_colors}},
|
||||
"DEF:smb=$rrd:user_smb:AVERAGE",
|
||||
@tmp);
|
||||
$err = RRDs::error;
|
||||
print("ERROR: while graphing $PNG_DIR" . "$PNG2: $err\n") if $err;
|
||||
if(lc($config->{enable_zoom}) eq "y") {
|
||||
($width, $height) = split('x', $config->{graph_size}->{zoom});
|
||||
RRDs::graph("$PNG_DIR" . "$PNG2z",
|
||||
"--title=$config->{graphs}->{_user2} ($tf->{nwhen}$tf->{twhen})",
|
||||
"--start=-$tf->{nwhen}$tf->{twhen}",
|
||||
"--imgformat=PNG",
|
||||
"--vertical-label=Users",
|
||||
"--width=$width",
|
||||
"--height=$height",
|
||||
@riglim,
|
||||
"--lower-limit=0",
|
||||
@{$cgi->{version12}},
|
||||
@{$cgi->{version12_small}},
|
||||
@{$colors->{graph_colors}},
|
||||
"DEF:smb=$rrd:user_smb:AVERAGE",
|
||||
@tmpz);
|
||||
$err = RRDs::error;
|
||||
print("ERROR: while graphing $PNG_DIR" . "$PNG2z: $err\n") if $err;
|
||||
}
|
||||
if($title || ($silent =~ /imagetag/ && $graph =~ /user2/)) {
|
||||
if(lc($config->{enable_zoom}) eq "y") {
|
||||
if(lc($config->{disable_javascript_void}) eq "y") {
|
||||
print(" <a href=\"" . $config->{url} . $config->{imgs_dir} . $PNG2z . "\"><img src='" . $config->{url} . $config->{imgs_dir} . $PNG2 . "' border='0'></a>\n");
|
||||
}
|
||||
else {
|
||||
print(" <a href=\"javascript:void(window.open('" . $config->{url} . $config->{imgs_dir} . $PNG2z . "','','width=" . ($width + 115) . ",height=" . ($height + 100) . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . $config->{imgs_dir} . $PNG2 . "' border='0'></a>\n");
|
||||
}
|
||||
} else {
|
||||
print(" <img src='" . $config->{url} . $config->{imgs_dir} . $PNG2 . "'>\n");
|
||||
}
|
||||
}
|
||||
|
||||
undef(@riglim);
|
||||
if(trim($rigid[2]) eq 1) {
|
||||
push(@riglim, "--upper-limit=" . trim($limit[2]));
|
||||
} else {
|
||||
if(trim($rigid[2]) eq 2) {
|
||||
push(@riglim, "--upper-limit=" . trim($limit[2]));
|
||||
push(@riglim, "--rigid");
|
||||
}
|
||||
}
|
||||
undef(@tmp);
|
||||
undef(@tmpz);
|
||||
push(@tmp, "AREA:mac#EE4444:Netatalk");
|
||||
push(@tmp, "GPRINT:mac:LAST: Current\\: %3.0lf\\n");
|
||||
push(@tmp, "LINE1:mac#EE0000");
|
||||
push(@tmpz, "AREA:mac#EE4444:Netatalk");
|
||||
push(@tmpz, "LINE2:mac#EE0000");
|
||||
($width, $height) = split('x', $config->{graph_size}->{small});
|
||||
if($silent =~ /imagetag/) {
|
||||
($width, $height) = split('x', $config->{graph_size}->{remote}) if $silent eq "imagetag";
|
||||
($width, $height) = split('x', $config->{graph_size}->{main}) if $silent eq "imagetagbig";
|
||||
@tmp = @tmpz;
|
||||
push(@tmp, "COMMENT: \\n");
|
||||
push(@tmp, "COMMENT: \\n");
|
||||
push(@tmp, "COMMENT: \\n");
|
||||
}
|
||||
RRDs::graph("$PNG_DIR" . "$PNG3",
|
||||
"--title=$config->{graphs}->{_user3} ($tf->{nwhen}$tf->{twhen})",
|
||||
"--start=-$tf->{nwhen}$tf->{twhen}",
|
||||
"--imgformat=PNG",
|
||||
"--vertical-label=Users",
|
||||
"--width=$width",
|
||||
"--height=$height",
|
||||
@riglim,
|
||||
"--lower-limit=0",
|
||||
@{$cgi->{version12}},
|
||||
@{$cgi->{version12_small}},
|
||||
@{$colors->{graph_colors}},
|
||||
"DEF:mac=$rrd:user_mac:AVERAGE",
|
||||
@tmp);
|
||||
$err = RRDs::error;
|
||||
print("ERROR: while graphing $PNG_DIR" . "$PNG3: $err\n") if $err;
|
||||
if(lc($config->{enable_zoom}) eq "y") {
|
||||
($width, $height) = split('x', $config->{graph_size}->{zoom});
|
||||
RRDs::graph("$PNG_DIR" . "$PNG3z",
|
||||
"--title=$config->{graphs}->{_user3} ($tf->{nwhen}$tf->{twhen})",
|
||||
"--start=-$tf->{nwhen}$tf->{twhen}",
|
||||
"--imgformat=PNG",
|
||||
"--vertical-label=Users",
|
||||
"--width=$width",
|
||||
"--height=$height",
|
||||
@riglim,
|
||||
"--lower-limit=0",
|
||||
@{$cgi->{version12}},
|
||||
@{$cgi->{version12_small}},
|
||||
@{$colors->{graph_colors}},
|
||||
"DEF:mac=$rrd:user_mac:AVERAGE",
|
||||
@tmpz);
|
||||
$err = RRDs::error;
|
||||
print("ERROR: while graphing $PNG_DIR" . "$PNG3z: $err\n") if $err;
|
||||
}
|
||||
if($title || ($silent =~ /imagetag/ && $graph =~ /user3/)) {
|
||||
if(lc($config->{enable_zoom}) eq "y") {
|
||||
if(lc($config->{disable_javascript_void}) eq "y") {
|
||||
print(" <a href=\"" . $config->{url} . $config->{imgs_dir} . $PNG3z . "\"><img src='" . $config->{url} . $config->{imgs_dir} . $PNG3 . "' border='0'></a>\n");
|
||||
}
|
||||
else {
|
||||
print(" <a href=\"javascript:void(window.open('" . $config->{url} . $config->{imgs_dir} . $PNG3z . "','','width=" . ($width + 115) . ",height=" . ($height + 100) . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . $config->{imgs_dir} . $PNG3 . "' border='0'></a>\n");
|
||||
}
|
||||
} else {
|
||||
print(" <img src='" . $config->{url} . $config->{imgs_dir} . $PNG3 . "'>\n");
|
||||
}
|
||||
}
|
||||
|
||||
if($title) {
|
||||
print(" </td>\n");
|
||||
print(" </tr>\n");
|
||||
main::graph_footer();
|
||||
}
|
||||
print(" <br>\n");
|
||||
return;
|
||||
}
|
||||
|
||||
1;
|
|
@ -242,12 +242,10 @@ alert_rootfs_script = /path/to/script.sh
|
|||
|
||||
# USER graph
|
||||
# -----------------------------------------------------------------------------
|
||||
user1_rigid = 0
|
||||
user1_limit = 100
|
||||
user2_rigid = 0
|
||||
user2_limit = 100
|
||||
user3_rigid = 0
|
||||
user3_limit = 100
|
||||
<user>
|
||||
rigid = 0, 0, 0
|
||||
limit = 1000, 1000, 1000
|
||||
</user>
|
||||
|
||||
|
||||
# APACHE graph
|
||||
|
|
Loading…
Reference in New Issue