2013-01-04 08:27:05 +00:00
#
# Monitorix - A lightweight system monitoring tool.
#
2015-02-12 15:27:03 +00:00
# Copyright (C) 2005-2015 by Jordi Sanfeliu <jordi@fibranet.cat>
2013-01-04 08:27:05 +00:00
#
# 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 system ;
use strict ;
use warnings ;
use Monitorix ;
use RRDs ;
use Exporter 'import' ;
our @ EXPORT = qw( system_init system_update system_cgi ) ;
sub system_init {
my $ myself = ( caller ( 0 ) ) [ 3 ] ;
my ( $ package , $ config , $ debug ) = @ _ ;
my $ rrd = $ config - > { base_lib } . $ package . ".rrd" ;
2013-01-22 12:15:20 +00:00
my $ system = $ config - > { system } ;
2013-01-04 08:27:05 +00:00
2013-11-04 09:27:48 +00:00
my $ info ;
my @ rra ;
my @ tmp ;
my $ n ;
my @ average ;
my @ min ;
my @ max ;
my @ last ;
if ( - e $ rrd ) {
$ info = RRDs:: info ( $ rrd ) ;
for my $ key ( keys %$ info ) {
if ( index ( $ key , 'rra[' ) == 0 ) {
if ( index ( $ key , '.rows' ) != - 1 ) {
push ( @ rra , substr ( $ key , 4 , index ( $ key , ']' ) - 4 ) ) ;
}
}
}
2013-11-04 15:51:19 +00:00
if ( scalar ( @ rra ) < 12 + ( 4 * $ config - > { max_historic_years } ) ) {
2013-11-04 09:27:48 +00:00
logger ( "$myself: Detected size mismatch between 'max_historic_years' (" . $ config - > { max_historic_years } . ") and $rrd (" . ( ( scalar ( @ rra ) - 12 ) / 4 ) . "). Resizing it accordingly. All historical data will be lost. Backup file created." ) ;
rename ( $ rrd , "$rrd.bak" ) ;
}
}
2013-01-04 08:27:05 +00:00
if ( ! ( - e $ rrd ) ) {
logger ( "Creating '$rrd' file." ) ;
2013-11-04 09:27:48 +00:00
for ( $ n = 1 ; $ n <= $ config - > { max_historic_years } ; $ n + + ) {
push ( @ average , "RRA:AVERAGE:0.5:1440:" . ( 365 * $ n ) ) ;
push ( @ min , "RRA:MIN:0.5:1440:" . ( 365 * $ n ) ) ;
push ( @ max , "RRA:MAX:0.5:1440:" . ( 365 * $ n ) ) ;
push ( @ last , "RRA:LAST:0.5:1440:" . ( 365 * $ n ) ) ;
}
2013-01-04 08:27:05 +00:00
eval {
RRDs:: create ( $ rrd ,
"--step=60" ,
"DS:system_load1:GAUGE:120:0:U" ,
"DS:system_load5:GAUGE:120:0:U" ,
"DS:system_load15:GAUGE:120:0:U" ,
"DS:system_nproc:GAUGE:120:0:U" ,
"DS:system_npslp:GAUGE:120:0:U" ,
"DS:system_nprun:GAUGE:120:0:U" ,
"DS:system_npwio:GAUGE:120:0:U" ,
"DS:system_npzom:GAUGE:120:0:U" ,
"DS:system_npstp:GAUGE:120:0:U" ,
"DS:system_npswp:GAUGE:120:0:U" ,
"DS:system_mtotl:GAUGE:120:0:U" ,
"DS:system_mbuff:GAUGE:120:0:U" ,
"DS:system_mcach:GAUGE:120:0:U" ,
"DS:system_mfree:GAUGE:120:0:U" ,
"DS:system_macti:GAUGE:120:0:U" ,
"DS:system_minac:GAUGE:120:0:U" ,
"DS:system_val01:GAUGE:120:0:U" ,
"DS:system_val02:GAUGE:120:0:U" ,
"DS:system_val03:GAUGE:120:0:U" ,
"DS:system_val04:GAUGE:120:0:U" ,
"DS:system_val05:GAUGE:120:0:U" ,
"RRA:AVERAGE:0.5:1:1440" ,
"RRA:AVERAGE:0.5:30:336" ,
"RRA:AVERAGE:0.5:60:744" ,
2013-11-04 09:27:48 +00:00
@ average ,
2013-01-04 08:27:05 +00:00
"RRA:MIN:0.5:1:1440" ,
"RRA:MIN:0.5:30:336" ,
"RRA:MIN:0.5:60:744" ,
2013-11-04 09:27:48 +00:00
@ min ,
2013-01-04 08:27:05 +00:00
"RRA:MAX:0.5:1:1440" ,
"RRA:MAX:0.5:30:336" ,
"RRA:MAX:0.5:60:744" ,
2013-11-04 09:27:48 +00:00
@ max ,
2013-01-04 08:27:05 +00:00
"RRA:LAST:0.5:1:1440" ,
"RRA:LAST:0.5:30:336" ,
"RRA:LAST:0.5:60:744" ,
2013-11-04 09:27:48 +00:00
@ last ,
2013-01-04 08:27:05 +00:00
) ;
} ;
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 ;
}
}
# check dependencies
2013-01-23 11:06:19 +00:00
if ( lc ( $ system - > { alerts } - > { loadavg_enabled } ) eq "y" ) {
2013-01-22 12:15:20 +00:00
if ( ! - x $ system - > { alerts } - > { loadavg_script } ) {
2013-01-22 14:11:34 +00:00
logger ( "$myself: ERROR: script '$system->{alerts}->{loadavg_script}' doesn't exist or don't has execution permissions." ) ;
2013-01-04 08:27:05 +00:00
}
}
2013-01-22 14:35:31 +00:00
$ config - > { system_hist_alert1 } = 0 ;
2013-01-04 08:27:05 +00:00
push ( @ { $ config - > { func_update } } , $ package ) ;
logger ( "$myself: Ok" ) if $ debug ;
}
sub system_update {
my $ myself = ( caller ( 0 ) ) [ 3 ] ;
my ( $ package , $ config , $ debug ) = @ _ ;
my $ rrd = $ config - > { base_lib } . $ package . ".rrd" ;
2013-01-22 12:15:20 +00:00
my $ system = $ config - > { system } ;
2013-01-04 08:27:05 +00:00
2013-08-21 10:05:41 +01:00
my $ load1 = 0 ;
my $ load5 = 0 ;
my $ load15 = 0 ;
my $ nproc = 0 ;
my $ npslp = 0 ;
my $ nprun = 0 ;
my $ npwio = 0 ;
my $ npzom = 0 ;
my $ npstp = 0 ;
my $ npswp = 0 ;
my $ mtotl = 0 ;
my $ mbuff = 0 ;
my $ mcach = 0 ;
my $ mfree = 0 ;
my $ macti = 0 ;
my $ minac = 0 ;
2013-01-04 08:27:05 +00:00
my $ val01 = 0 ;
my $ val02 = 0 ;
my $ val03 = 0 ;
my $ val04 = 0 ;
my $ val05 = 0 ;
my $ rrdata = "N" ;
$ npwio = $ npzom = $ npstp = $ npswp = 0 ;
if ( $ config - > { os } eq "Linux" ) {
open ( IN , "/proc/loadavg" ) ;
while ( <IN> ) {
if ( /^(\d+\.\d+) (\d+\.\d+) (\d+\.\d+) (\d+)\/(\d+)/ ) {
$ load1 = $ 1 ;
$ load5 = $ 2 ;
$ load15 = $ 3 ;
$ nprun = $ 4 ;
$ npslp = $ 5 ;
}
}
close ( IN ) ;
$ nproc = $ npslp + $ nprun ;
open ( IN , "/proc/meminfo" ) ;
while ( <IN> ) {
if ( /^MemTotal:\s+(\d+) kB$/ ) {
$ mtotl = $ 1 ;
next ;
}
if ( /^MemFree:\s+(\d+) kB$/ ) {
$ mfree = $ 1 ;
next ;
}
if ( /^Buffers:\s+(\d+) kB$/ ) {
$ mbuff = $ 1 ;
next ;
}
if ( /^Cached:\s+(\d+) kB$/ ) {
$ mcach = $ 1 ;
last ;
}
}
close ( IN ) ;
2015-05-19 14:32:34 +01:00
$ macti = $ minac = 0 ;
2013-01-04 08:27:05 +00:00
} elsif ( $ config - > { os } eq "FreeBSD" ) {
my $ page_size ;
open ( IN , "sysctl -n vm.loadavg |" ) ;
while ( <IN> ) {
if ( /^\{ (\d+\.\d+) (\d+\.\d+) (\d+\.\d+) \}$/ ) {
$ load1 = $ 1 ;
$ load5 = $ 2 ;
$ load15 = $ 3 ;
}
}
close ( IN ) ;
open ( IN , "sysctl vm.vmtotal |" ) ;
while ( <IN> ) {
if ( /^Processes:\s+\(RUNQ:\s+(\d+) Disk.*? Sleep:\s+(\d+)\)$/ ) {
$ nprun = $ 1 ;
$ npslp = $ 2 ;
}
2013-03-04 10:28:47 +00:00
if ( /^(Free Memory Pages:|Free Memory:)\s+(\d+)K$/ ) {
$ mfree = $ 2 ;
2013-01-04 08:27:05 +00:00
}
}
close ( IN ) ;
$ nproc = $ npslp + $ nprun ;
$ mtotl = `sysctl -n hw.realmem` ;
$ mbuff = `sysctl -n vfs.bufspace` ;
$ mcach = `sysctl -n vm.stats.vm.v_cache_count` ;
chomp ( $ mbuff ) ;
$ mbuff = $ mbuff / 1024 ;
chomp ( $ mtotl ) ;
$ mtotl = $ mtotl / 1024 ;
$ page_size = `sysctl -n vm.stats.vm.v_page_size` ;
$ macti = `sysctl -n vm.stats.vm.v_active_count` ;
$ minac = `sysctl -n vm.stats.vm.v_inactive_count` ;
chomp ( $ page_size , $ mcach , $ macti , $ minac ) ;
$ mcach = ( $ page_size * $ mcach ) / 1024 ;
$ macti = ( $ page_size * $ macti ) / 1024 ;
$ minac = ( $ page_size * $ minac ) / 1024 ;
} elsif ( $ config - > { os } eq "OpenBSD" || $ config - > { os } eq "NetBSD" ) {
open ( IN , "sysctl -n vm.loadavg |" ) ;
while ( <IN> ) {
if ( /^(\d+\.\d+) (\d+\.\d+) (\d+\.\d+)$/ ) {
$ load1 = $ 1 ;
$ load5 = $ 2 ;
$ load15 = $ 3 ;
}
}
close ( IN ) ;
open ( IN , "top -b |" ) ;
while ( <IN> ) {
if ( / processes:/ ) {
$ _ =~ s/:/,/ ;
my ( @ tmp ) = split ( ',' , $ _ ) ;
foreach ( @ tmp ) {
my ( $ num , $ desc ) = split ( ' ' , $ _ ) ;
$ nproc = $ num unless $ desc ne "processes" ;
if ( grep { $ _ eq $ desc } ( "idle" , "sleeping" , "stopped" , "zombie" ) ) {
$ npslp += $ num ;
}
if ( $ desc eq "running" || $ desc eq "on" ) {
$ nprun += $ num ;
}
}
}
if ( $ config - > { os } eq "OpenBSD" ) {
if ( /^Memory: Real: (\d+)\w\/\d+\w act\/tot Free: (\d+)\w / ) {
$ macti = $ 1 ;
$ mfree = $ 2 ;
$ macti = int ( $ macti ) * 1024 ;
$ mfree = int ( $ mfree ) * 1024 ;
last ;
}
}
if ( $ config - > { os } eq "NetBSD" ) {
if ( /^Memory: (\d+)\w Act, .*, (\d+)\w Free/ ) {
$ macti = $ 1 ;
$ mfree = $ 2 ;
$ macti = int ( $ macti ) * 1024 ;
$ mfree = int ( $ mfree ) * 1024 ;
last ;
}
}
}
close ( IN ) ;
$ mtotl = `sysctl -n hw.physmem` ;
chomp ( $ mtotl ) ;
$ mtotl = $ mtotl / 1024 ;
}
chomp (
$ load1 ,
$ load5 ,
$ load15 ,
$ nproc ,
$ npslp ,
$ nprun ,
$ npwio ,
$ npzom ,
$ npstp ,
$ npswp ,
$ mtotl ,
$ mbuff ,
$ mcach ,
$ mfree ,
$ macti ,
$ minac ,
) ;
# SYSTEM alert
2013-01-23 11:06:19 +00:00
if ( lc ( $ system - > { alerts } - > { loadavg_enabled } ) eq "y" ) {
2013-01-22 12:15:20 +00:00
if ( ! $ system - > { alerts } - > { loadavg_threshold } || $ load15 < $ system - > { alerts } - > { loadavg_threshold } ) {
2013-01-22 14:35:31 +00:00
$ config - > { system_hist_alert1 } = 0 ;
2013-01-04 08:27:05 +00:00
} else {
2013-01-22 14:35:31 +00:00
if ( ! $ config - > { system_hist_alert1 } ) {
$ config - > { system_hist_alert1 } = time ;
2013-01-04 08:27:05 +00:00
}
2013-04-29 14:30:20 +01:00
if ( $ config - > { system_hist_alert1 } > 0 && ( time - $ config - > { system_hist_alert1 } ) >= $ system - > { alerts } - > { loadavg_timeintvl } ) {
2013-01-22 12:15:20 +00:00
if ( - x $ system - > { alerts } - > { loadavg_script } ) {
2013-01-23 11:06:19 +00:00
logger ( "$myself: ALERT: executing script '$system->{alerts}->{loadavg_script}'." ) ;
2013-01-22 12:15:20 +00:00
system ( $ system - > { alerts } - > { loadavg_script } . " " . $ system - > { alerts } - > { loadavg_timeintvl } . " " . $ system - > { alerts } - > { loadavg_threshold } . " " . $ load15 ) ;
} else {
2013-01-23 11:06:19 +00:00
logger ( "$myself: ERROR: script '$system->{alerts}->{loadavg_script}' doesn't exist or don't has execution permissions." ) ;
2013-01-04 08:27:05 +00:00
}
2013-01-22 14:35:31 +00:00
$ config - > { system_hist_alert1 } = time ;
2013-01-04 08:27:05 +00:00
}
}
}
$ rrdata . = ":$load1:$load5:$load15:$nproc:$npslp:$nprun:$npwio:$npzom:$npstp:$npswp:$mtotl:$mbuff:$mcach:$mfree:$macti:$minac:$val01:$val02:$val03:$val04:$val05" ;
RRDs:: update ( $ rrd , $ rrdata ) ;
logger ( "$myself: $rrdata" ) if $ debug ;
my $ err = RRDs:: error ;
logger ( "ERROR: while updating $rrd: $err" ) if $ err ;
}
sub system_cgi {
my ( $ package , $ config , $ cgi ) = @ _ ;
my $ system = $ config - > { system } ;
2014-06-18 09:06:10 +01:00
my @ rigid = split ( ',' , ( $ system - > { rigid } || "" ) ) ;
my @ limit = split ( ',' , ( $ system - > { limit } || "" ) ) ;
2013-01-04 08:27:05 +00:00
my $ tf = $ cgi - > { tf } ;
my $ colors = $ cgi - > { colors } ;
my $ graph = $ cgi - > { graph } ;
my $ silent = $ cgi - > { silent } ;
2013-07-29 17:03:13 +01:00
my $ zoom = "--zoom=" . $ config - > { global_zoom } ;
2014-12-30 14:57:00 +00:00
my % rrd = (
'new' = > \ & RRDs:: graphv ,
'old' = > \ & RRDs:: graph ,
) ;
my $ version = "new" ;
my $ pic ;
my $ picz ;
2014-12-30 15:44:10 +00:00
my $ picz_width ;
my $ picz_height ;
2013-01-04 08:27:05 +00:00
my $ u = "" ;
my $ width ;
my $ height ;
my @ riglim ;
my @ tmp ;
my @ tmpz ;
2013-06-28 11:40:11 +01:00
my @ CDEF ,
2013-01-04 08:27:05 +00:00
my $ n ;
my $ err ;
2014-12-30 14:57:00 +00:00
$ version = "old" if $ RRDs:: VERSION < 1.3 ;
2013-01-04 08:27:05 +00:00
my $ rrd = $ config - > { base_lib } . $ package . ".rrd" ;
my $ title = $ config - > { graph_title } - > { $ package } ;
my $ PNG_DIR = $ config - > { base_dir } . "/" . $ config - > { imgs_dir } ;
2014-12-30 14:57:00 +00:00
2013-01-04 08:27:05 +00:00
$ title = ! $ silent ? $ title : "" ;
my $ total_mem ;
if ( $ config - > { os } eq "Linux" ) {
$ total_mem = `grep -w MemTotal: /proc/meminfo | awk '{print \$2}'` ;
chomp ( $ total_mem ) ;
} elsif ( grep { $ _ eq $ config - > { os } } ( "FreeBSD" , "OpenBSD" , "NetBSD" ) ) {
$ total_mem = `/sbin/sysctl -n hw.physmem` ; # in bytes
chomp ( $ total_mem ) ;
$ total_mem = int ( $ total_mem / 1024 ) ; # in KB
}
$ total_mem = int ( $ total_mem / 1024 ) ; # in MB
# 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 ( " CPU load average Memory usage in MB Processes\n" ) ;
print ( "Time 1min 5min 15min Used Cached Buffers Total Run\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 ( $ load1 , $ load5 , $ load15 , $ nproc , $ npslp , $ nprun , $ mtotl , $ buff , $ cach , $ free ) = @$ line ;
$ buff /= 1024 ;
$ cach /= 1024 ;
$ free /= 1024 ;
@ row = ( $ load1 , $ load5 , $ load15 , $ total_mem - $ free , $ cach , $ buff , $ nproc , $ nprun ) ;
$ time = $ time - ( 1 / $ tf - > { ts } ) ;
printf ( " %2d$tf->{tc} %4.1f %4.1f %4.1f %6d %6d %6d %5d %5d \n" , $ time , @ row ) ;
}
print ( "\n" ) ;
print ( " system uptime: " . get_uptime ( $ config ) . "\n" ) ;
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 ) ;
}
2014-06-17 18:30:06 +01:00
@ riglim = @ { setup_riglim ( $ rigid [ 0 ] , $ limit [ 0 ] ) } ;
2013-01-04 08:27:05 +00:00
my $ uptimeline ;
if ( $ RRDs:: VERSION > 1.2 ) {
$ uptimeline = "COMMENT:system uptime\\: " . get_uptime ( $ config ) . "\\c" ;
} else {
$ uptimeline = "COMMENT:system uptime: " . get_uptime ( $ config ) . "\\c" ;
}
if ( $ title ) {
print ( " <tr>\n" ) ;
print ( " <td bgcolor='$colors->{title_bg_color}'>\n" ) ;
}
push ( @ tmp , "AREA:load1#4444EE: 1 min average" ) ;
push ( @ tmp , "GPRINT:load1:LAST: Current\\: %4.2lf" ) ;
push ( @ tmp , "GPRINT:load1:AVERAGE: Average\\: %4.2lf" ) ;
push ( @ tmp , "GPRINT:load1:MIN: Min\\: %4.2lf" ) ;
push ( @ tmp , "GPRINT:load1:MAX: Max\\: %4.2lf\\n" ) ;
push ( @ tmp , "LINE1:load1#0000EE" ) ;
push ( @ tmp , "LINE1:load5#EEEE00: 5 min average" ) ;
push ( @ tmp , "GPRINT:load5:LAST: Current\\: %4.2lf" ) ;
push ( @ tmp , "GPRINT:load5:AVERAGE: Average\\: %4.2lf" ) ;
push ( @ tmp , "GPRINT:load5:MIN: Min\\: %4.2lf" ) ;
push ( @ tmp , "GPRINT:load5:MAX: Max\\: %4.2lf\\n" ) ;
push ( @ tmp , "LINE1:load15#00EEEE:15 min average" ) ;
push ( @ tmp , "GPRINT:load15:LAST: Current\\: %4.2lf" ) ;
push ( @ tmp , "GPRINT:load15:AVERAGE: Average\\: %4.2lf" ) ;
push ( @ tmp , "GPRINT:load15:MIN: Min\\: %4.2lf" ) ;
push ( @ tmp , "GPRINT:load15:MAX: Max\\: %4.2lf\\n" ) ;
push ( @ tmpz , "AREA:load1#4444EE: 1 min average" ) ;
push ( @ tmpz , "LINE1:load1#0000EE" ) ;
push ( @ tmpz , "LINE1:load5#EEEE00: 5 min average" ) ;
push ( @ tmpz , "LINE1:load15#00EEEE:15 min average" ) ;
2013-06-28 11:40:11 +01:00
if ( lc ( $ config - > { show_gaps } ) eq "y" ) {
push ( @ tmp , "AREA:wrongdata#$colors->{gap}:" ) ;
push ( @ tmpz , "AREA:wrongdata#$colors->{gap}:" ) ;
push ( @ CDEF , "CDEF:wrongdata=allvalues,UN,INF,UNKN,IF" ) ;
}
2013-01-04 08:27:05 +00:00
if ( $ config - > { os } eq "FreeBSD" ) {
push ( @ tmp , "COMMENT: \\n" ) ;
}
( $ 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 ;
push ( @ tmp , "COMMENT: \\n" ) ;
}
2014-12-30 14:57:00 +00:00
$ pic = $ rrd { $ version } - > ( "$PNG_DIR" . "$PNG1" ,
2013-01-04 08:27:05 +00:00
"--title=$config->{graphs}->{_system1} ($tf->{nwhen}$tf->{twhen})" ,
"--start=-$tf->{nwhen}$tf->{twhen}" ,
"--imgformat=PNG" ,
"--vertical-label=Load average" ,
"--width=$width" ,
"--height=$height" ,
@ riglim ,
2013-07-29 17:03:13 +01:00
$ zoom ,
2013-01-04 08:27:05 +00:00
@ { $ cgi - > { version12 } } ,
@ { $ colors - > { graph_colors } } ,
"DEF:load1=$rrd:system_load1:AVERAGE" ,
"DEF:load5=$rrd:system_load5:AVERAGE" ,
"DEF:load15=$rrd:system_load15:AVERAGE" ,
2013-06-28 11:40:11 +01:00
"CDEF:allvalues=load1,load5,load15,+,+" ,
@ CDEF ,
2013-01-04 08:27:05 +00:00
@ tmp ,
"COMMENT: \\n" ,
$ uptimeline ) ;
$ 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 } ) ;
2014-12-30 14:57:00 +00:00
$ picz = $ rrd { $ version } - > ( "$PNG_DIR" . "$PNG1z" ,
2013-01-04 08:27:05 +00:00
"--title=$config->{graphs}->{_system1} ($tf->{nwhen}$tf->{twhen})" ,
"--start=-$tf->{nwhen}$tf->{twhen}" ,
"--imgformat=PNG" ,
"--vertical-label=Load average" ,
"--width=$width" ,
"--height=$height" ,
@ riglim ,
2014-12-30 14:57:00 +00:00
$ zoom ,
2013-01-04 08:27:05 +00:00
@ { $ cgi - > { version12 } } ,
@ { $ colors - > { graph_colors } } ,
"DEF:load1=$rrd:system_load1:AVERAGE" ,
"DEF:load5=$rrd:system_load5:AVERAGE" ,
"DEF:load15=$rrd:system_load15:AVERAGE" ,
2013-06-28 11:40:11 +01:00
"CDEF:allvalues=load1,load5,load15,+,+" ,
@ CDEF ,
2013-01-04 08:27:05 +00:00
@ tmpz ) ;
$ err = RRDs:: error ;
print ( "ERROR: while graphing $PNG_DIR" . "$PNG1z: $err\n" ) if $ err ;
}
if ( $ title || ( $ silent =~ /imagetag/ && $ graph =~ /system1/ ) ) {
if ( lc ( $ config - > { enable_zoom } ) eq "y" ) {
if ( lc ( $ config - > { disable_javascript_void } ) eq "y" ) {
2013-02-11 10:36:17 +00:00
print ( " <a href=\"" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG1z . "\"><img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG1 . "' border='0'></a>\n" ) ;
2015-02-12 15:27:03 +00:00
} else {
2014-12-30 15:44:10 +00:00
if ( $ version eq "new" ) {
$ picz_width = $ picz - > { image_width } * $ config - > { global_zoom } ;
$ picz_height = $ picz - > { image_height } * $ config - > { global_zoom } ;
} else {
$ picz_width = $ width + 115 ;
$ picz_height = $ height + 100 ;
}
print ( " <a href=\"javascript:void(window.open('" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG1z . "','','width=" . $ picz_width . ",height=" . $ picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG1 . "' border='0'></a>\n" ) ;
2013-01-04 08:27:05 +00:00
}
} else {
2013-02-11 10:36:17 +00:00
print ( " <img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG1 . "'>\n" ) ;
2013-01-04 08:27:05 +00:00
}
}
2013-01-04 09:40:31 +00:00
2013-01-04 08:27:05 +00:00
if ( $ title ) {
print ( " </td>\n" ) ;
print ( " <td bgcolor='" . $ colors - > { title_bg_color } . "'>\n" ) ;
}
2014-06-17 18:30:06 +01:00
@ riglim = @ { setup_riglim ( $ rigid [ 1 ] , $ limit [ 1 ] ) } ;
2013-01-04 08:27:05 +00:00
undef ( @ tmp ) ;
undef ( @ tmpz ) ;
2013-06-28 11:40:11 +01:00
undef ( @ CDEF ) ;
2013-01-04 08:27:05 +00:00
push ( @ tmp , "AREA:npslp#44AAEE:Sleeping" ) ;
push ( @ tmp , "AREA:nprun#EE4444:Running" ) ;
push ( @ tmp , "LINE1:nprun#EE0000" ) ;
push ( @ tmp , "LINE1:npslp#00EEEE" ) ;
push ( @ tmp , "LINE1:nproc#EEEE00:Processes" ) ;
2013-06-28 11:40:11 +01:00
if ( lc ( $ config - > { show_gaps } ) eq "y" ) {
push ( @ tmp , "AREA:wrongdata#$colors->{gap}:" ) ;
push ( @ CDEF , "CDEF:wrongdata=allvalues,UN,INF,UNKN,IF" ) ;
}
2013-01-04 08:27:05 +00:00
( $ 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" ;
push ( @ tmp , "COMMENT: \\n" ) ;
push ( @ tmp , "COMMENT: \\n" ) ;
}
2014-12-30 15:27:00 +00:00
$ pic = $ rrd { $ version } - > ( "$PNG_DIR" . "$PNG2" ,
2013-01-04 08:27:05 +00:00
"--title=$config->{graphs}->{_system2} ($tf->{nwhen}$tf->{twhen})" ,
"--start=-$tf->{nwhen}$tf->{twhen}" ,
"--imgformat=PNG" ,
"--vertical-label=Processes" ,
"--width=$width" ,
"--height=$height" ,
@ riglim ,
2013-07-29 17:03:13 +01:00
$ zoom ,
2013-01-04 08:27:05 +00:00
@ { $ cgi - > { version12 } } ,
@ { $ cgi - > { version12_small } } ,
@ { $ colors - > { graph_colors } } ,
"DEF:nproc=$rrd:system_nproc:AVERAGE" ,
"DEF:npslp=$rrd:system_npslp:AVERAGE" ,
"DEF:nprun=$rrd:system_nprun:AVERAGE" ,
2013-06-28 11:40:11 +01:00
"CDEF:allvalues=nproc,npslp,nprun,+,+" ,
@ CDEF ,
2013-01-04 08:27:05 +00:00
@ tmp ,
"COMMENT: \\n" ) ;
$ 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 } ) ;
2014-12-30 15:27:00 +00:00
$ picz = $ rrd { $ version } - > ( "$PNG_DIR" . "$PNG2z" ,
2013-01-04 08:27:05 +00:00
"--title=$config->{graphs}->{_system2} ($tf->{nwhen}$tf->{twhen})" ,
"--start=-$tf->{nwhen}$tf->{twhen}" ,
"--imgformat=PNG" ,
"--vertical-label=Processes" ,
"--width=$width" ,
"--height=$height" ,
@ riglim ,
2014-12-30 15:27:00 +00:00
$ zoom ,
2013-01-04 08:27:05 +00:00
@ { $ cgi - > { version12 } } ,
@ { $ cgi - > { version12_small } } ,
@ { $ colors - > { graph_colors } } ,
"DEF:nproc=$rrd:system_nproc:AVERAGE" ,
"DEF:npslp=$rrd:system_npslp:AVERAGE" ,
"DEF:nprun=$rrd:system_nprun:AVERAGE" ,
2013-06-28 11:40:11 +01:00
"CDEF:allvalues=nproc,npslp,nprun,+,+" ,
@ CDEF ,
2013-01-04 08:27:05 +00:00
@ tmp ) ;
$ err = RRDs:: error ;
print ( "ERROR: while graphing $PNG_DIR" . "$PNG2z: $err\n" ) if $ err ;
}
if ( $ title || ( $ silent =~ /imagetag/ && $ graph =~ /system2/ ) ) {
if ( lc ( $ config - > { enable_zoom } ) eq "y" ) {
if ( lc ( $ config - > { disable_javascript_void } ) eq "y" ) {
2013-02-11 10:36:17 +00:00
print ( " <a href=\"" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG2z . "\"><img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG2 . "' border='0'></a>\n" ) ;
2015-02-12 15:27:03 +00:00
} else {
2014-12-30 15:44:10 +00:00
if ( $ version eq "new" ) {
$ picz_width = $ picz - > { image_width } * $ config - > { global_zoom } ;
$ picz_height = $ picz - > { image_height } * $ config - > { global_zoom } ;
} else {
$ picz_width = $ width + 115 ;
$ picz_height = $ height + 100 ;
}
print ( " <a href=\"javascript:void(window.open('" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG2z . "','','width=" . $ picz_width . ",height=" . $ picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG2 . "' border='0'></a>\n" ) ;
2013-01-04 08:27:05 +00:00
}
} else {
2013-02-11 10:36:17 +00:00
print ( " <img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG2 . "'>\n" ) ;
2013-01-04 08:27:05 +00:00
}
}
undef ( @ tmp ) ;
undef ( @ tmpz ) ;
2013-06-28 11:40:11 +01:00
undef ( @ CDEF ) ;
2013-01-04 08:27:05 +00:00
if ( $ config - > { os } eq "Linux" ) {
push ( @ tmp , "AREA:m_mused#EE4444:Used" ) ;
push ( @ tmp , "AREA:m_mcach#44EE44:Cached" ) ;
push ( @ tmp , "AREA:m_mbuff#CCCCCC:Buffers" ) ;
push ( @ tmp , "LINE1:m_mbuff#888888" ) ;
push ( @ tmp , "LINE1:m_mcach#00EE00" ) ;
push ( @ tmp , "LINE1:m_mused#EE0000" ) ;
} elsif ( $ config - > { os } eq "FreeBSD" ) {
push ( @ tmp , "AREA:m_mused#EE4444:Used" ) ;
push ( @ tmp , "AREA:m_mcach#44EE44:Cached" ) ;
push ( @ tmp , "AREA:m_mbuff#CCCCCC:Buffers" ) ;
push ( @ tmp , "AREA:m_macti#EEEE44:Active" ) ;
push ( @ tmp , "AREA:m_minac#4444EE:Inactive" ) ;
push ( @ tmp , "LINE1:m_minac#0000EE" ) ;
push ( @ tmp , "LINE1:m_macti#EEEE00" ) ;
push ( @ tmp , "LINE1:m_mbuff#888888" ) ;
push ( @ tmp , "LINE1:m_mcach#00EE00" ) ;
push ( @ tmp , "LINE1:m_mused#EE0000" ) ;
} elsif ( $ config - > { os } eq "OpenBSD" || $ config - > { os } eq "NetBSD" ) {
push ( @ tmp , "AREA:m_mused#EE4444:Used" ) ;
push ( @ tmp , "AREA:m_macti#44EE44:Active" ) ;
push ( @ tmp , "LINE1:m_macti#00EE00" ) ;
push ( @ tmp , "LINE1:m_mused#EE0000" ) ;
}
2013-06-28 11:40:11 +01:00
if ( lc ( $ config - > { show_gaps } ) eq "y" ) {
push ( @ tmp , "AREA:wrongdata#$colors->{gap}:" ) ;
push ( @ CDEF , "CDEF:wrongdata=allvalues,UN,INF,UNKN,IF" ) ;
}
2013-01-04 08:27:05 +00:00
( $ 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" ;
push ( @ tmp , "COMMENT: \\n" ) ;
push ( @ tmp , "COMMENT: \\n" ) ;
}
2014-12-30 15:27:00 +00:00
$ pic = $ rrd { $ version } - > ( "$PNG_DIR" . "$PNG3" ,
2013-01-04 08:27:05 +00:00
"--title=$config->{graphs}->{_system3} (${total_mem}MB) ($tf->{nwhen}$tf->{twhen})" ,
"--start=-$tf->{nwhen}$tf->{twhen}" ,
"--imgformat=PNG" ,
"--vertical-label=Megabytes" ,
"--width=$width" ,
"--height=$height" ,
"--upper-limit=$total_mem" ,
"--lower-limit=0" ,
"--base=1024" ,
2013-07-29 17:03:13 +01:00
$ zoom ,
2013-01-04 08:27:05 +00:00
@ { $ cgi - > { version12 } } ,
@ { $ cgi - > { version12_small } } ,
@ { $ colors - > { graph_colors } } ,
"DEF:mtotl=$rrd:system_mtotl:AVERAGE" ,
"DEF:mbuff=$rrd:system_mbuff:AVERAGE" ,
"DEF:mcach=$rrd:system_mcach:AVERAGE" ,
"DEF:mfree=$rrd:system_mfree:AVERAGE" ,
"DEF:macti=$rrd:system_macti:AVERAGE" ,
"DEF:minac=$rrd:system_minac:AVERAGE" ,
"CDEF:m_mtotl=mtotl,1024,/" ,
"CDEF:m_mbuff=mbuff,1024,/" ,
"CDEF:m_mcach=mcach,1024,/" ,
"CDEF:m_mused=m_mtotl,mfree,1024,/,-" ,
"CDEF:m_macti=macti,1024,/" ,
"CDEF:m_minac=minac,1024,/" ,
2013-06-28 11:40:11 +01:00
"CDEF:allvalues=mtotl,mbuff,mcach,mfree,macti,minac,+,+,+,+,+" ,
@ CDEF ,
2013-01-04 08:27:05 +00:00
@ tmp ,
"COMMENT: \\n" ) ;
$ 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 } ) ;
2014-12-30 15:27:00 +00:00
$ picz = $ rrd { $ version } - > ( "$PNG_DIR" . "$PNG3z" ,
2013-01-04 08:27:05 +00:00
"--title=$config->{graphs}->{_system3} (${total_mem}MB) ($tf->{nwhen}$tf->{twhen})" ,
"--start=-$tf->{nwhen}$tf->{twhen}" ,
"--imgformat=PNG" ,
"--vertical-label=Megabytes" ,
"--width=$width" ,
"--height=$height" ,
"--upper-limit=$total_mem" ,
"--lower-limit=0" ,
"--base=1024" ,
2014-12-30 15:27:00 +00:00
$ zoom ,
2013-01-04 08:27:05 +00:00
@ { $ cgi - > { version12 } } ,
@ { $ cgi - > { version12_small } } ,
@ { $ colors - > { graph_colors } } ,
"DEF:mtotl=$rrd:system_mtotl:AVERAGE" ,
"DEF:mbuff=$rrd:system_mbuff:AVERAGE" ,
"DEF:mcach=$rrd:system_mcach:AVERAGE" ,
"DEF:mfree=$rrd:system_mfree:AVERAGE" ,
"DEF:macti=$rrd:system_macti:AVERAGE" ,
"DEF:minac=$rrd:system_minac:AVERAGE" ,
"CDEF:m_mtotl=mtotl,1024,/" ,
"CDEF:m_mbuff=mbuff,1024,/" ,
"CDEF:m_mcach=mcach,1024,/" ,
"CDEF:m_mused=m_mtotl,mfree,1024,/,-" ,
"CDEF:m_macti=macti,1024,/" ,
"CDEF:m_minac=minac,1024,/" ,
2013-06-28 11:40:11 +01:00
"CDEF:allvalues=mtotl,mbuff,mcach,mfree,macti,minac,+,+,+,+,+" ,
@ CDEF ,
2013-01-04 08:27:05 +00:00
@ tmp ) ;
$ err = RRDs:: error ;
print ( "ERROR: while graphing $PNG_DIR" . "$PNG3z: $err\n" ) if $ err ;
}
if ( $ title || ( $ silent =~ /imagetag/ && $ graph =~ /system3/ ) ) {
if ( lc ( $ config - > { enable_zoom } ) eq "y" ) {
if ( lc ( $ config - > { disable_javascript_void } ) eq "y" ) {
2013-02-11 10:36:17 +00:00
print ( " <a href=\"" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG3z . "\"><img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG3 . "' border='0'></a>\n" ) ;
2015-02-12 15:27:03 +00:00
} else {
2014-12-30 15:44:10 +00:00
if ( $ version eq "new" ) {
$ picz_width = $ picz - > { image_width } * $ config - > { global_zoom } ;
$ picz_height = $ picz - > { image_height } * $ config - > { global_zoom } ;
} else {
$ picz_width = $ width + 115 ;
$ picz_height = $ height + 100 ;
}
print ( " <a href=\"javascript:void(window.open('" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG3z . "','','width=" . $ picz_width . ",height=" . $ picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG3 . "' border='0'></a>\n" ) ;
2013-01-04 08:27:05 +00:00
}
} else {
2013-02-11 10:36:17 +00:00
print ( " <img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ PNG3 . "'>\n" ) ;
2013-01-04 08:27:05 +00:00
}
}
if ( $ title ) {
print ( " </td>\n" ) ;
print ( " </tr>\n" ) ;
main:: graph_footer ( ) ;
}
print ( " <br>\n" ) ;
return ;
}
sub get_uptime {
my $ config = shift ;
my $ str ;
my $ uptime ;
if ( $ config - > { os } eq "Linux" ) {
open ( IN , "/proc/uptime" ) ;
( $ uptime , undef ) = split ( ' ' , <IN> ) ;
close ( IN ) ;
} elsif ( $ config - > { os } eq "FreeBSD" ) {
open ( IN , "/sbin/sysctl -n kern.boottime |" ) ;
( undef , undef , undef , $ uptime ) = split ( ' ' , <IN> ) ;
close ( IN ) ;
$ uptime =~ s/,// ;
$ uptime = time - int ( $ uptime ) ;
} elsif ( $ config - > { os } eq "OpenBSD" || $ config - > { os } eq "NetBSD" ) {
open ( IN , "/sbin/sysctl -n kern.boottime |" ) ;
$ uptime = <IN> ;
close ( IN ) ;
chomp ( $ uptime ) ;
$ uptime = time - int ( $ uptime ) ;
}
2013-06-12 16:20:04 +01:00
return uptime2str ( $ uptime ) ;
2013-01-04 08:27:05 +00:00
}
1 ;