2013-01-04 08:27:05 +00:00
#
# Monitorix - A lightweight system monitoring tool.
#
2020-02-21 09:36:35 +00:00
# Copyright (C) 2005-2020 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 port ;
use strict ;
use warnings ;
use Monitorix ;
use RRDs ;
2017-07-05 10:43:20 +01:00
use POSIX qw( strftime setlocale LC_ALL ) ;
2013-01-04 08:27:05 +00:00
use Exporter 'import' ;
our @ EXPORT = qw( port_init port_update port_cgi ) ;
2017-07-05 10:43:20 +01:00
# Force a standard locale
2017-07-05 10:47:16 +01:00
$ ENV { LANG } = "" ;
2017-07-05 10:43:20 +01:00
setlocale ( LC_ALL , "C" ) ;
2013-01-04 08:27:05 +00:00
sub port_init {
my $ myself = ( caller ( 0 ) ) [ 3 ] ;
my ( $ package , $ config , $ debug ) = @ _ ;
my $ rrd = $ config - > { base_lib } . $ package . ".rrd" ;
my $ port = $ config - > { port } ;
2015-02-16 17:00:45 +00:00
my $ cmd ;
2013-01-04 08:27:05 +00:00
my $ info ;
my @ ds ;
2013-10-29 10:29:24 +00:00
my @ rra ;
2013-01-04 08:27:05 +00:00
my @ tmp ;
my $ n ;
2013-10-29 10:29:24 +00:00
my @ average ;
my @ min ;
my @ max ;
my @ last ;
2014-06-26 10:50:17 +01:00
my $ table = $ config - > { ip_default_table } ;
2013-01-04 08:27:05 +00:00
if ( - e $ rrd ) {
$ info = RRDs:: info ( $ rrd ) ;
for my $ key ( keys %$ info ) {
if ( index ( $ key , 'ds[' ) == 0 ) {
if ( index ( $ key , '.type' ) != - 1 ) {
push ( @ ds , substr ( $ key , 3 , index ( $ key , ']' ) - 3 ) ) ;
}
}
2013-10-29 10:29:24 +00:00
if ( index ( $ key , 'rra[' ) == 0 ) {
if ( index ( $ key , '.rows' ) != - 1 ) {
push ( @ rra , substr ( $ key , 4 , index ( $ key , ']' ) - 4 ) ) ;
}
}
2013-01-04 08:27:05 +00:00
}
2013-01-29 11:45:33 +00:00
if ( scalar ( @ ds ) / 4 != $ port - > { max } ) {
2013-10-29 10:29:24 +00:00
logger ( "$myself: Detected size mismatch between 'max = $port->{max}' and $rrd (" . scalar ( @ ds ) / 4 . "). Resizing it accordingly. All historical data will be lost. Backup file created." ) ;
rename ( $ rrd , "$rrd.bak" ) ;
}
2013-11-04 15:51:19 +00:00
if ( scalar ( @ rra ) < 12 + ( 4 * $ config - > { max_historic_years } ) ) {
2013-10-29 10:29:24 +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." ) ;
2013-01-04 08:27:05 +00:00
rename ( $ rrd , "$rrd.bak" ) ;
}
}
if ( ! ( - e $ rrd ) ) {
logger ( "Creating '$rrd' file." ) ;
2013-10-29 10:29:24 +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
for ( $ n = 0 ; $ n < $ port - > { max } ; $ n + + ) {
2013-01-29 11:45:33 +00:00
push ( @ tmp , "DS:port" . $ n . "_i_in:GAUGE:120:0:U" ) ;
push ( @ tmp , "DS:port" . $ n . "_i_out:GAUGE:120:0:U" ) ;
push ( @ tmp , "DS:port" . $ n . "_o_in:GAUGE:120:0:U" ) ;
push ( @ tmp , "DS:port" . $ n . "_o_out:GAUGE:120:0:U" ) ;
2013-01-04 08:27:05 +00:00
}
eval {
RRDs:: create ( $ rrd ,
"--step=60" ,
@ tmp ,
"RRA:AVERAGE:0.5:1:1440" ,
"RRA:AVERAGE:0.5:30:336" ,
"RRA:AVERAGE:0.5:60:744" ,
2013-10-29 10:29:24 +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-10-29 10:29:24 +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-10-29 10:29:24 +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-10-29 10:29:24 +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 ;
}
}
2014-01-30 14:16:53 +00:00
if ( scalar ( my @ pls = split ( ',' , $ port - > { list } ) ) > $ port - > { max } ) {
logger ( "$myself: WARNING: 'max' option indicates less ports than really defined in 'list'." ) ;
}
2019-11-21 08:51:31 +00:00
if ( lc ( $ config - > { use_external_firewall } || "" ) eq "n" ) {
2019-11-20 15:51:52 +00:00
if ( $ config - > { os } eq "Linux" ) {
my $ num ;
my @ line ;
2014-02-20 11:54:42 +00:00
2019-11-20 15:51:52 +00:00
# set the iptables rules for each defined port
my @ pl = split ( ',' , $ port - > { list } ) ;
for ( $ n = 0 ; $ n < min ( $ port - > { max } , scalar ( @ pl ) ) ; $ n + + ) {
$ pl [ $ n ] = trim ( $ pl [ $ n ] ) ;
my ( $ np ) = ( $ pl [ $ n ] =~ m/^(\d+).*?/ ) ;
if ( ! $ port - > { desc } - > { $ pl [ $ n ] } ) {
logger ( "$myself: port number '$np' listed but not defined." ) ;
2015-03-03 08:19:32 +00:00
next ;
}
2019-11-20 15:51:52 +00:00
# support for port range (i.e: 49152:65534)
if ( index ( $ pl [ $ n ] , ":" ) != - 1 ) {
( $ np ) = ( $ pl [ $ n ] =~ m/^(\d+:\d+).*?/ ) ;
}
if ( $ pl [ $ n ] && $ np ) {
my $ p = trim ( lc ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 1 ] ) ) || "" ;
if ( ! grep { $ _ eq $ p } ( "tcp" , "udp" , "tcp6" , "udp6" ) ) {
logger ( "$myself: Invalid protocol name '$p' in port '$pl[$n]'." ) ;
2015-08-03 10:22:21 +01:00
next ;
}
2019-11-20 15:51:52 +00:00
$ cmd = "iptables" . $ config - > { iptables_wait_lock } ;
if ( grep { $ _ eq $ p } ( "tcp6" , "udp6" ) ) {
if ( lc ( $ config - > { ipv6_disabled } || "" ) eq "y" ) {
logger ( "$myself: IPv6 is explicitly disabled, you shouldn't want to monitor 'tcp6' or 'udp6' protocols." ) ;
next ;
}
$ cmd = "ip6tables" . $ config - > { iptables_wait_lock } ;
$ p =~ s/6// ;
}
my $ conn = trim ( lc ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 2 ] ) ) ;
if ( $ conn eq "in" || $ conn eq "in/out" ) {
system ( "$cmd -t $table -N monitorix_IN_$n 2>/dev/null" ) ;
system ( "$cmd -t $table -I INPUT -p $p --sport 1024:65535 --dport $np -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j monitorix_IN_$n -c 0 0" ) ;
system ( "$cmd -t $table -I OUTPUT -p $p --sport $np --dport 1024:65535 -m conntrack --ctstate ESTABLISHED,RELATED -j monitorix_IN_$n -c 0 0" ) ;
}
if ( $ conn eq "out" || $ conn eq "in/out" ) {
system ( "$cmd -t $table -N monitorix_OUT_$n 2>/dev/null" ) ;
system ( "$cmd -t $table -I INPUT -p $p --sport $np --dport 1024:65535 -m conntrack --ctstate ESTABLISHED,RELATED -j monitorix_OUT_$n -c 0 0" ) ;
system ( "$cmd -t $table -I OUTPUT -p $p --sport 1024:65535 --dport $np -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j monitorix_OUT_$n -c 0 0" ) ;
}
if ( $ conn ne "in" && $ conn ne "out" && $ conn ne "in/out" ) {
logger ( "$myself: Invalid connection type '$conn'; must be 'in', 'out' or 'in/out'." ) ;
}
2013-01-25 12:04:03 +00:00
}
2013-01-04 08:27:05 +00:00
}
}
}
if ( grep { $ _ eq $ config - > { os } } ( "FreeBSD" , "OpenBSD" , "NetBSD" ) ) {
# set the ipfw rules for each defined port
my @ pl = split ( ',' , $ port - > { list } ) ;
2014-02-04 16:27:23 +00:00
for ( $ n = 0 ; $ n < min ( $ port - > { max } , scalar ( @ pl ) ) ; $ n + + ) {
2013-01-04 08:27:05 +00:00
$ pl [ $ n ] = trim ( $ pl [ $ n ] ) ;
2014-01-30 14:16:53 +00:00
my ( $ np ) = ( $ pl [ $ n ] =~ m/^(\d+).*?/ ) ;
if ( $ pl [ $ n ] && $ np ) {
2013-01-04 08:27:05 +00:00
my $ p = lc ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 1 ] ) || "all" ;
2014-02-04 16:27:23 +00:00
# in/out not supported yet FIXME
2015-02-16 17:00:45 +00:00
$ p =~ s/6// ; # tcp6, udp6, ... not supported
2014-01-30 14:16:53 +00:00
system ( "ipfw -q add $port->{rule} count $p from me $np to any" ) ;
system ( "ipfw -q add $port->{rule} count $p from any to me $np" ) ;
2013-01-04 08:27:05 +00:00
}
}
}
$ config - > { port_hist_in } = ( ) ;
$ config - > { port_hist_out } = ( ) ;
push ( @ { $ config - > { func_update } } , $ package ) ;
logger ( "$myself: Ok" ) if $ debug ;
}
sub port_update {
my $ myself = ( caller ( 0 ) ) [ 3 ] ;
my ( $ package , $ config , $ debug ) = @ _ ;
my $ rrd = $ config - > { base_lib } . $ package . ".rrd" ;
my $ port = $ config - > { port } ;
2013-01-29 11:45:33 +00:00
my @ i_in ;
my @ i_out ;
my @ o_in ;
my @ o_out ;
2014-06-26 10:50:17 +01:00
my $ table = $ config - > { ip_default_table } ;
2013-01-04 08:27:05 +00:00
my $ n ;
my $ rrdata = "N" ;
if ( $ config - > { os } eq "Linux" ) {
2015-02-16 17:00:45 +00:00
my @ data ;
my $ l ;
2015-02-17 14:36:42 +00:00
my $ cmd ;
my $ cmd6 ;
2015-02-16 17:00:45 +00:00
2015-02-17 14:36:42 +00:00
$ cmd = "iptables" . $ config - > { iptables_wait_lock } ;
$ cmd6 = "ip6tables" . $ config - > { iptables_wait_lock } ;
2015-03-03 16:56:54 +00:00
open ( IN , "$cmd -t $table -nxvL INPUT 2>/dev/null |" ) ;
2015-02-16 17:00:45 +00:00
@ data = <IN> ;
close ( IN ) ;
2015-09-22 14:59:19 +01:00
if ( lc ( $ config - > { ipv6_disabled } || "" ) ne "y" ) {
2015-08-03 10:22:21 +01:00
open ( IN , "$cmd6 -t $table -nxvL INPUT 2>/dev/null |" ) ;
push ( @ data , <IN> ) ;
close ( IN ) ;
}
2015-02-16 17:00:45 +00:00
for ( $ l = 0 ; $ l < scalar ( @ data ) ; $ l + + ) {
2013-01-04 08:27:05 +00:00
for ( $ n = 0 ; $ n < $ port - > { max } ; $ n + + ) {
2013-01-29 11:45:33 +00:00
$ i_in [ $ n ] = 0 unless $ i_in [ $ n ] ;
$ o_in [ $ n ] = 0 unless $ o_in [ $ n ] ;
2015-02-16 17:00:45 +00:00
if ( $ data [ $ l ] =~ / monitorix_IN_$n / ) {
my ( undef , $ bytes ) = split ( ' ' , $ data [ $ l ] ) ;
2013-01-04 08:27:05 +00:00
chomp ( $ bytes ) ;
2013-01-29 11:45:33 +00:00
$ i_in [ $ n ] = $ bytes - ( $ config - > { port_hist_i_in } [ $ n ] || 0 ) ;
$ i_in [ $ n ] = 0 unless $ i_in [ $ n ] != $ bytes ;
$ config - > { port_hist_i_in } [ $ n ] = $ bytes ;
$ i_in [ $ n ] /= 60 ;
}
2015-02-16 17:00:45 +00:00
if ( $ data [ $ l ] =~ / monitorix_OUT_$n / ) {
my ( undef , $ bytes ) = split ( ' ' , $ data [ $ l ] ) ;
2013-01-29 11:45:33 +00:00
chomp ( $ bytes ) ;
$ o_in [ $ n ] = $ bytes - ( $ config - > { port_hist_o_in } [ $ n ] || 0 ) ;
$ o_in [ $ n ] = 0 unless $ o_in [ $ n ] != $ bytes ;
$ config - > { port_hist_o_in } [ $ n ] = $ bytes ;
$ o_in [ $ n ] /= 60 ;
2013-01-04 08:27:05 +00:00
}
}
}
2015-03-03 16:56:54 +00:00
open ( IN , "$cmd -t $table -nxvL OUTPUT 2>/dev/null |" ) ;
2015-02-16 17:00:45 +00:00
@ data = <IN> ;
close ( IN ) ;
2015-09-22 14:59:19 +01:00
if ( lc ( $ config - > { ipv6_disabled } || "" ) ne "y" ) {
2015-08-03 10:22:21 +01:00
open ( IN , "$cmd6 -t $table -nxvL OUTPUT 2>/dev/null |" ) ;
push ( @ data , <IN> ) ;
close ( IN ) ;
}
2015-02-16 17:00:45 +00:00
for ( $ l = 0 ; $ l < scalar ( @ data ) ; $ l + + ) {
2013-01-04 08:27:05 +00:00
for ( $ n = 0 ; $ n < $ port - > { max } ; $ n + + ) {
2013-01-29 11:45:33 +00:00
$ o_out [ $ n ] = 0 unless $ o_out [ $ n ] ;
$ i_out [ $ n ] = 0 unless $ i_out [ $ n ] ;
2015-02-16 17:00:45 +00:00
if ( $ data [ $ l ] =~ / monitorix_OUT_$n / ) {
my ( undef , $ bytes ) = split ( ' ' , $ data [ $ l ] ) ;
2013-01-29 11:45:33 +00:00
chomp ( $ bytes ) ;
$ o_out [ $ n ] = $ bytes - ( $ config - > { port_hist_o_out } [ $ n ] || 0 ) ;
$ o_out [ $ n ] = 0 unless $ o_out [ $ n ] != $ bytes ;
$ config - > { port_hist_o_out } [ $ n ] = $ bytes ;
$ o_out [ $ n ] /= 60 ;
}
2015-02-16 17:00:45 +00:00
if ( $ data [ $ l ] =~ / monitorix_IN_$n / ) {
my ( undef , $ bytes ) = split ( ' ' , $ data [ $ l ] ) ;
2013-01-04 08:27:05 +00:00
chomp ( $ bytes ) ;
2013-01-29 11:45:33 +00:00
$ i_out [ $ n ] = $ bytes - ( $ config - > { port_hist_i_out } [ $ n ] || 0 ) ;
$ i_out [ $ n ] = 0 unless $ i_out [ $ n ] != $ bytes ;
$ config - > { port_hist_i_out } [ $ n ] = $ bytes ;
$ i_out [ $ n ] /= 60 ;
2013-01-04 08:27:05 +00:00
}
}
}
}
if ( grep { $ _ eq $ config - > { os } } ( "FreeBSD" , "OpenBSD" , "NetBSD" ) ) {
my @ pl = split ( ',' , $ port - > { list } ) ;
open ( IN , "ipfw show $port->{rule} 2>/dev/null |" ) ;
while ( <IN> ) {
for ( $ n = 0 ; $ n < $ port - > { max } ; $ n + + ) {
2013-01-29 11:45:33 +00:00
$ i_in [ $ n ] = 0 unless $ i_in [ $ n ] ;
$ o_in [ $ n ] = 0 unless $ o_in [ $ n ] ;
2013-01-04 08:27:05 +00:00
$ pl [ $ n ] = trim ( $ pl [ $ n ] ) ;
2014-01-30 14:16:53 +00:00
my ( $ np ) = ( $ pl [ $ n ] =~ m/^(\d+).*?/ ) ;
if ( / from any to me dst-port $np$/ ) {
2013-01-04 08:27:05 +00:00
my ( undef , undef , $ bytes ) = split ( ' ' , $ _ ) ;
chomp ( $ bytes ) ;
2013-03-08 12:05:55 +00:00
$ i_in [ $ n ] = $ bytes - ( $ config - > { port_hist_i_in } [ $ n ] || 0 ) ;
$ i_in [ $ n ] = 0 unless $ i_in [ $ n ] != $ bytes ;
$ config - > { port_hist_i_in } [ $ n ] = $ bytes ;
$ i_in [ $ n ] /= 60 ;
2013-01-04 08:27:05 +00:00
}
2013-01-29 11:45:33 +00:00
$ o_out [ $ n ] = 0 unless $ o_out [ $ n ] ;
$ i_out [ $ n ] = 0 unless $ i_out [ $ n ] ;
2014-01-30 14:16:53 +00:00
if ( / from me $np to any$/ ) {
2013-01-04 08:27:05 +00:00
my ( undef , undef , $ bytes ) = split ( ' ' , $ _ ) ;
chomp ( $ bytes ) ;
2013-03-08 12:05:55 +00:00
$ i_out [ $ n ] = $ bytes - ( $ config - > { port_hist_i_out } [ $ n ] || 0 ) ;
$ i_out [ $ n ] = 0 unless $ i_out [ $ n ] != $ bytes ;
$ config - > { port_hist_i_out } [ $ n ] = $ bytes ;
$ i_out [ $ n ] /= 60 ;
2013-01-04 08:27:05 +00:00
}
}
}
close ( IN ) ;
}
for ( $ n = 0 ; $ n < $ port - > { max } ; $ n + + ) {
2013-01-29 11:45:33 +00:00
$ rrdata . = ":$i_in[$n]:$i_out[$n]:$o_in[$n]:$o_out[$n]" ;
2013-01-04 08:27:05 +00:00
}
RRDs:: update ( $ rrd , $ rrdata ) ;
logger ( "$myself: $rrdata" ) if $ debug ;
my $ err = RRDs:: error ;
logger ( "ERROR: while updating $rrd: $err" ) if $ err ;
}
sub port_cgi {
my ( $ package , $ config , $ cgi ) = @ _ ;
2017-08-28 16:50:15 +01:00
my @ output ;
2013-01-04 08:27:05 +00:00
my $ port = $ config - > { port } ;
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 } ;
2015-01-21 16:33:21 +00:00
my % rrd = (
'new' = > \ & RRDs:: graphv ,
'old' = > \ & RRDs:: graph ,
) ;
my $ version = "new" ;
my $ pic ;
my $ picz ;
my $ picz_width ;
my $ picz_height ;
2013-01-04 08:27:05 +00:00
my $ u = "" ;
my $ width ;
my $ height ;
2019-05-08 11:54:03 +01:00
my @ extra ;
2013-01-04 08:27:05 +00:00
my @ riglim ;
my @ warning ;
2016-01-25 17:35:30 +00:00
my @ IMG ;
my @ IMGz ;
2013-01-04 08:27:05 +00:00
my $ name ;
my @ tmp ;
my @ tmpz ;
my @ CDEF ;
my $ T = "B" ;
my $ vlabel = "bytes/s" ;
my $ n ;
my $ n2 ;
2013-01-29 11:45:33 +00:00
my $ n3 ;
2013-01-04 08:27:05 +00:00
my $ str ;
my $ err ;
2015-01-21 16:33:21 +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 } ;
2016-01-25 17:35:30 +00:00
my $ IMG_DIR = $ config - > { base_dir } . "/" . $ config - > { imgs_dir } ;
my $ imgfmt_uc = uc ( $ config - > { image_format } ) ;
my $ imgfmt_lc = lc ( $ config - > { image_format } ) ;
2019-05-08 11:54:03 +01:00
foreach my $ i ( split ( ',' , $ config - > { rrdtool_extra_options } || "" ) ) {
push ( @ extra , trim ( $ i ) ) if trim ( $ i ) ;
}
2013-01-04 08:27:05 +00:00
$ title = ! $ silent ? $ title : "" ;
if ( lc ( $ config - > { netstats_in_bps } ) eq "y" ) {
$ T = "b" ;
$ vlabel = "bits/s" ;
}
# text mode
#
if ( lc ( $ config - > { iface_mode } ) eq "text" ) {
if ( $ title ) {
2017-08-28 16:50:15 +01:00
push ( @ output , main:: graph_header ( $ title , 2 ) ) ;
push ( @ output , " <tr>\n" ) ;
2020-11-20 08:15:58 +00:00
push ( @ output , " <td>\n" ) ;
2013-01-04 08:27:05 +00:00
}
my ( undef , undef , undef , $ data ) = RRDs:: fetch ( "$rrd" ,
2019-11-22 07:46:02 +00:00
"--resolution=$tf->{res}" ,
2013-01-04 08:27:05 +00:00
"--start=-$tf->{nwhen}$tf->{twhen}" ,
2019-11-22 07:46:02 +00:00
"AVERAGE" ) ;
2013-01-04 08:27:05 +00:00
$ err = RRDs:: error ;
2017-08-28 16:50:15 +01:00
push ( @ output , "ERROR: while fetching $rrd: $err\n" ) if $ err ;
2013-01-04 08:27:05 +00:00
my $ line1 ;
my $ line2 ;
2017-08-28 16:50:15 +01:00
push ( @ output , " <pre style='font-size: 12px; color: $colors->{fg_color}';>\n" ) ;
push ( @ output , " " ) ;
2014-02-04 16:27:23 +00:00
my $ max = min ( $ port - > { max } , scalar ( my @ pl = split ( ',' , $ port - > { list } ) ) ) ;
for ( $ n = 0 ; $ n < $ max ; $ n + + ) {
2013-01-04 08:27:05 +00:00
$ pl [ $ n ] = trim ( $ pl [ $ n ] ) ;
my $ pn = trim ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 0 ] ) ;
2013-01-29 11:45:33 +00:00
my $ pc = trim ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 2 ] ) ;
foreach ( split ( '/' , $ pc ) ) {
2017-08-28 16:50:15 +01:00
push ( @ output , sprintf ( " %-5s %10s" , $ pl [ $ n ] , uc ( trim ( $ _ ) ) . "-" . $ pn ) ) ;
2013-01-29 11:45:33 +00:00
$ line1 . = " K$T/s_I K$T/s_O" ;
$ line2 . = "-------------------" ;
}
2013-01-04 08:27:05 +00:00
}
2017-08-28 16:50:15 +01:00
push ( @ output , "\n" ) ;
push ( @ output , "Time$line1\n" ) ;
push ( @ output , "----$line2 \n" ) ;
2013-01-04 08:27:05 +00:00
my $ line ;
my @ row ;
my $ time ;
my $ n2 ;
my $ from ;
my $ to ;
for ( $ n = 0 , $ time = $ tf - > { tb } ; $ n < ( $ tf - > { tb } * $ tf - > { ts } ) ; $ n + + ) {
$ line = @$ data [ $ n ] ;
$ time = $ time - ( 1 / $ tf - > { ts } ) ;
2017-08-28 16:50:15 +01:00
push ( @ output , sprintf ( " %2d$tf->{tc} " , $ time ) ) ;
2014-02-04 16:27:23 +00:00
for ( $ n2 = 0 ; $ n2 < $ max ; $ n2 + + ) {
2013-01-29 11:45:33 +00:00
$ pl [ $ n2 ] = trim ( $ pl [ $ n2 ] ) ;
my $ pc = trim ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n2 ] } ) ) [ 2 ] ) ;
$ from = $ n2 * 4 ;
$ to = $ from + 3 ;
my ( $ i_in , $ i_out , $ o_in , $ o_out ) = @$ line [ $ from .. $ to ] ;
my $ k_i_in = ( $ i_in || 0 ) / 1024 ;
my $ k_i_out = ( $ i_out || 0 ) / 1024 ;
my $ k_o_in = ( $ o_in || 0 ) / 1024 ;
my $ k_o_out = ( $ o_out || 0 ) / 1024 ;
2013-01-04 08:27:05 +00:00
if ( lc ( $ config - > { netstats_in_bps } ) eq "y" ) {
2013-01-29 11:45:33 +00:00
$ k_i_in *= 8 ;
$ k_i_out *= 8 ;
$ k_o_in *= 8 ;
$ k_o_out *= 8 ;
}
foreach ( split ( '/' , $ pc ) ) {
if ( lc ( $ _ ) eq "in" ) {
@ row = ( $ k_i_in , $ k_i_out ) ;
2017-08-28 16:50:15 +01:00
push ( @ output , sprintf ( " %6d %6d " , @ row ) ) ;
2013-01-29 11:45:33 +00:00
}
if ( lc ( $ _ ) eq "out" ) {
@ row = ( $ k_o_in , $ k_o_out ) ;
2017-08-28 16:50:15 +01:00
push ( @ output , sprintf ( " %6d %6d " , @ row ) ) ;
2013-01-29 11:45:33 +00:00
}
2013-01-04 08:27:05 +00:00
}
}
2017-08-28 16:50:15 +01:00
push ( @ output , "\n" ) ;
2013-01-04 08:27:05 +00:00
}
2017-08-28 16:50:15 +01:00
push ( @ output , " </pre>\n" ) ;
2013-01-04 08:27:05 +00:00
if ( $ title ) {
2017-08-28 16:50:15 +01:00
push ( @ output , " </td>\n" ) ;
push ( @ output , " </tr>\n" ) ;
push ( @ output , main:: graph_footer ( ) ) ;
2013-01-04 08:27:05 +00:00
}
2017-08-28 16:50:15 +01:00
push ( @ output , " <br>\n" ) ;
return @ output ;
2013-01-04 08:27:05 +00:00
}
# 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 = "" ;
}
2013-04-24 14:34:09 +01:00
my $ max = min ( $ port - > { max } , scalar ( my @ pl = split ( ',' , $ port - > { list } ) ) ) ;
2013-01-29 11:45:33 +00:00
for ( $ n = 0 ; $ n < $ max ; $ n + + ) {
$ pl [ $ n ] = trim ( $ pl [ $ n ] ) ;
2014-02-20 11:54:42 +00:00
next if ! $ port - > { desc } - > { $ pl [ $ n ] } ;
2013-01-29 11:45:33 +00:00
my $ pc = trim ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 2 ] ) ;
foreach my $ conn ( split ( '/' , $ pc ) ) {
2016-01-25 17:35:30 +00:00
$ str = $ u . $ package . $ n . substr ( $ conn , 0 , 1 ) . "." . $ tf - > { when } . ".$imgfmt_lc" ;
push ( @ IMG , $ str ) ;
unlink ( "$IMG_DIR" . $ str ) ;
2013-01-29 11:45:33 +00:00
if ( lc ( $ config - > { enable_zoom } ) eq "y" ) {
2016-01-25 17:35:30 +00:00
$ str = $ u . $ package . $ n . substr ( $ conn , 0 , 1 ) . "z." . $ tf - > { when } . ".$imgfmt_lc" ;
push ( @ IMGz , $ str ) ;
unlink ( "$IMG_DIR" . $ str ) ;
2013-01-29 11:45:33 +00:00
}
2013-01-04 08:27:05 +00:00
}
}
2013-01-29 11:45:33 +00:00
$ n = $ n3 = 0 ;
$ n2 = 1 ;
while ( $ n < $ max ) {
2013-04-24 14:34:09 +01:00
next unless $ pl [ $ n ] ;
2014-02-20 11:54:42 +00:00
2017-05-10 16:07:48 +01:00
# continue if port was listed but not defined
2014-02-20 11:54:42 +00:00
if ( ! $ port - > { desc } - > { $ pl [ $ n ] } ) {
$ n + + ;
next ;
}
2013-01-04 08:27:05 +00:00
if ( $ title ) {
if ( $ n == 0 ) {
2017-08-28 16:50:15 +01:00
push ( @ output , main:: graph_header ( $ title , $ port - > { graphs_per_row } ) ) ;
2014-01-28 09:50:05 +00:00
}
if ( $ n2 == 1 ) {
2017-08-28 16:50:15 +01:00
push ( @ output , " <tr>\n" ) ;
2013-01-04 08:27:05 +00:00
}
}
2013-01-29 11:45:33 +00:00
my $ pc = trim ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 2 ] ) ;
foreach my $ pcon ( split ( '/' , $ pc ) ) {
2013-01-04 08:27:05 +00:00
if ( $ title ) {
2020-11-20 08:15:58 +00:00
push ( @ output , " <td>\n" ) ;
2013-01-04 08:27:05 +00:00
}
my $ pnum ;
$ pl [ $ n ] = trim ( $ pl [ $ n ] ) ;
2015-06-30 16:54:26 +01:00
my $ num = ( $ pl [ $ n ] =~ m/^(\d+).*?/ ) [ 0 ] ; # strips any suffix from port number
2013-01-04 08:27:05 +00:00
my $ pn = trim ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 0 ] ) ;
my $ pp = trim ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 1 ] ) ;
2015-02-16 17:00:45 +00:00
$ pp =~ s/6// ;
2013-01-25 12:04:03 +00:00
my $ prig = trim ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 3 ] ) ;
my $ plim = trim ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 4 ] ) ;
2017-05-11 08:49:45 +01:00
my $ plis = trim ( ( split ( ',' , $ port - > { desc } - > { $ pl [ $ n ] } ) ) [ 5 ] ) ;
2014-06-19 09:53:27 +01:00
@ riglim = @ { setup_riglim ( $ prig , $ plim ) } ;
2015-06-30 16:54:26 +01:00
# check if the network port is still listening
2013-01-04 08:27:05 +00:00
undef ( @ warning ) ;
2019-04-15 14:17:56 +01:00
if ( uc ( $ plis || "" ) eq "L" ) {
2017-05-11 08:49:45 +01:00
if ( $ config - > { os } eq "Linux" ) {
2020-06-22 14:43:44 +01:00
my $ cmd = $ port - > { cmd } || "" ;
2021-04-23 17:51:19 +01:00
if ( ! $ cmd ) {
$ cmd = "ss" ;
2020-06-22 14:43:44 +01:00
}
2021-04-23 17:51:19 +01:00
open ( IN , "$cmd -nl --$pp |" ) ;
while ( <IN> ) {
( undef , undef , undef , $ pnum ) = split ( ' ' , $ _ ) ;
chomp ( $ pnum ) ;
$ pnum =~ s/.*:// ;
if ( $ pnum eq $ num ) {
last ;
2020-06-22 14:43:44 +01:00
}
2013-01-04 08:27:05 +00:00
}
2021-04-23 17:51:19 +01:00
close ( IN ) ;
2017-05-11 08:49:45 +01:00
}
if ( grep { $ _ eq $ config - > { os } } ( "FreeBSD" , "OpenBSD" ) ) {
open ( IN , "netstat -anl -p $pp |" ) ;
while ( <IN> ) {
my $ stat ;
( undef , undef , undef , $ pnum , undef , $ stat ) = split ( ' ' , $ _ ) ;
chomp ( $ stat ) ;
if ( $ stat eq "LISTEN" ) {
chomp ( $ pnum ) ;
( $ pnum ) = ( $ pnum =~ m/^.*?(\.\d+$)/ ) ;
$ pnum =~ s/\.// ;
if ( $ pnum eq $ num ) {
last ;
}
}
}
close ( IN ) ;
}
if ( lc ( $ pcon ) ne "out" && $ pnum ne $ num ) {
push ( @ warning , $ colors - > { warning_color } ) ;
2013-01-04 08:27:05 +00:00
}
}
2013-01-29 11:45:33 +00:00
$ name = substr ( uc ( $ pcon ) . "-" . $ pn , 0 , 15 ) ;
2013-01-04 08:27:05 +00:00
undef ( @ tmp ) ;
undef ( @ tmpz ) ;
undef ( @ CDEF ) ;
2013-01-29 11:45:33 +00:00
if ( lc ( $ pcon ) eq "in" ) {
push ( @ tmp , "AREA:B_i_in#44EE44:Input" ) ;
push ( @ tmp , "AREA:B_i_out#4444EE:Output" ) ;
push ( @ tmp , "AREA:B_i_out#4444EE:" ) ;
push ( @ tmp , "AREA:B_i_in#44EE44:" ) ;
push ( @ tmp , "LINE1:B_i_out#0000EE" ) ;
push ( @ tmp , "LINE1:B_i_in#00EE00" ) ;
push ( @ tmpz , "AREA:B_i_in#44EE44:Input" ) ;
push ( @ tmpz , "AREA:B_i_out#4444EE:Output" ) ;
push ( @ tmpz , "AREA:B_i_out#4444EE:" ) ;
push ( @ tmpz , "AREA:B_i_in#44EE44:" ) ;
push ( @ tmpz , "LINE1:B_i_out#0000EE" ) ;
push ( @ tmpz , "LINE1:B_i_in#00EE00" ) ;
if ( lc ( $ config - > { netstats_in_bps } ) eq "y" ) {
push ( @ CDEF , "CDEF:B_i_in=i_in,8,*" ) ;
2020-07-13 09:14:05 +01:00
if ( lc ( $ config - > { netstats_mode } || "" ) eq "separated" ) {
push ( @ CDEF , "CDEF:B_i_out=i_out,8,*,-1,*" ) ;
} else {
push ( @ CDEF , "CDEF:B_i_out=i_out,8,*" ) ;
}
2013-01-29 11:45:33 +00:00
} else {
push ( @ CDEF , "CDEF:B_i_in=i_in" ) ;
2020-07-13 09:14:05 +01:00
if ( lc ( $ config - > { netstats_mode } || "" ) eq "separated" ) {
push ( @ CDEF , "CDEF:B_i_out=i_out,-1,*" ) ;
} else {
push ( @ CDEF , "CDEF:B_i_out=i_out" ) ;
}
2013-01-29 11:45:33 +00:00
}
}
if ( lc ( $ pcon ) eq "out" ) {
push ( @ tmp , "AREA:B_o_in#44EE44:Input" ) ;
push ( @ tmp , "AREA:B_o_out#4444EE:Output" ) ;
push ( @ tmp , "AREA:B_o_out#4444EE:" ) ;
push ( @ tmp , "AREA:B_o_in#44EE44:" ) ;
push ( @ tmp , "LINE1:B_o_out#0000EE" ) ;
push ( @ tmp , "LINE1:B_o_in#00EE00" ) ;
push ( @ tmpz , "AREA:B_o_in#44EE44:Input" ) ;
push ( @ tmpz , "AREA:B_o_out#4444EE:Output" ) ;
push ( @ tmpz , "AREA:B_o_out#4444EE:" ) ;
push ( @ tmpz , "AREA:B_o_in#44EE44:" ) ;
push ( @ tmpz , "LINE1:B_o_out#0000EE" ) ;
push ( @ tmpz , "LINE1:B_o_in#00EE00" ) ;
if ( lc ( $ config - > { netstats_in_bps } ) eq "y" ) {
push ( @ CDEF , "CDEF:B_o_in=o_in,8,*" ) ;
2020-07-13 09:14:05 +01:00
if ( lc ( $ config - > { netstats_mode } || "" ) eq "separated" ) {
push ( @ CDEF , "CDEF:B_o_out=o_out,8,*,-1,*" ) ;
} else {
push ( @ CDEF , "CDEF:B_o_out=o_out,8,*" ) ;
}
2013-01-29 11:45:33 +00:00
} else {
push ( @ CDEF , "CDEF:B_o_in=o_in" ) ;
2020-07-13 09:14:05 +01:00
if ( lc ( $ config - > { netstats_mode } || "" ) eq "separated" ) {
push ( @ CDEF , "CDEF:B_o_out=o_out,-1,*" ) ;
} else {
push ( @ CDEF , "CDEF:B_o_out=o_out" ) ;
}
2013-01-29 11:45:33 +00:00
}
2013-01-04 08:27:05 +00:00
}
2013-07-02 12:20:27 +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" ) ;
}
2020-05-28 08:58:45 +01:00
$ port - > { size } = "mini" if ! defined ( $ port - > { size } ) ;
( $ width , $ height ) = split ( 'x' , $ config - > { graph_size } - > { $ port - > { size } } ) ;
2013-01-04 08:27:05 +00:00
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" ) ;
push ( @ tmp , "COMMENT: \\n" ) ;
}
2016-01-25 17:35:30 +00:00
$ pic = $ rrd { $ version } - > ( "$IMG_DIR" . "$IMG[$n3]" ,
2013-01-04 08:27:05 +00:00
"--title=$name traffic ($tf->{nwhen}$tf->{twhen})" ,
"--start=-$tf->{nwhen}$tf->{twhen}" ,
2016-01-25 17:35:30 +00:00
"--imgformat=$imgfmt_uc" ,
2013-01-04 08:27:05 +00:00
"--vertical-label=$vlabel" ,
"--width=$width" ,
"--height=$height" ,
2019-05-08 11:54:03 +01:00
@ extra ,
2013-01-04 08:27:05 +00:00
@ 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 } } ,
@ warning ,
2013-01-29 11:45:33 +00:00
"DEF:i_in=$rrd:port" . $ n . "_i_in:AVERAGE" ,
"DEF:i_out=$rrd:port" . $ n . "_i_out:AVERAGE" ,
"DEF:o_in=$rrd:port" . $ n . "_o_in:AVERAGE" ,
"DEF:o_out=$rrd:port" . $ n . "_o_out:AVERAGE" ,
2013-07-02 12:20:27 +01:00
"CDEF:allvalues=i_in,i_out,o_in,o_out,+,+,+" ,
2013-01-04 08:27:05 +00:00
@ CDEF ,
@ tmp ) ;
$ err = RRDs:: error ;
2017-08-28 16:50:15 +01:00
push ( @ output , "ERROR: while graphing $IMG_DIR" . "$IMG[$n3]: $err\n" ) if $ err ;
2013-01-04 08:27:05 +00:00
if ( lc ( $ config - > { enable_zoom } ) eq "y" ) {
( $ width , $ height ) = split ( 'x' , $ config - > { graph_size } - > { zoom } ) ;
2016-01-25 17:35:30 +00:00
$ picz = $ rrd { $ version } - > ( "$IMG_DIR" . "$IMGz[$n3]" ,
2013-01-04 08:27:05 +00:00
"--title=$name traffic ($tf->{nwhen}$tf->{twhen})" ,
"--start=-$tf->{nwhen}$tf->{twhen}" ,
2016-01-25 17:35:30 +00:00
"--imgformat=$imgfmt_uc" ,
2013-01-04 08:27:05 +00:00
"--vertical-label=$vlabel" ,
"--width=$width" ,
"--height=$height" ,
2019-05-08 11:54:03 +01:00
@ extra ,
2013-01-04 08:27:05 +00:00
@ riglim ,
2015-01-21 16:33:21 +00:00
$ zoom ,
2013-01-04 08:27:05 +00:00
@ { $ cgi - > { version12 } } ,
@ { $ cgi - > { version12_small } } ,
@ { $ colors - > { graph_colors } } ,
@ warning ,
2013-01-29 11:45:33 +00:00
"DEF:i_in=$rrd:port" . $ n . "_i_in:AVERAGE" ,
"DEF:i_out=$rrd:port" . $ n . "_i_out:AVERAGE" ,
"DEF:o_in=$rrd:port" . $ n . "_o_in:AVERAGE" ,
"DEF:o_out=$rrd:port" . $ n . "_o_out:AVERAGE" ,
2013-07-02 12:20:27 +01:00
"CDEF:allvalues=i_in,i_out,o_in,o_out,+,+,+" ,
2013-01-04 08:27:05 +00:00
@ CDEF ,
@ tmpz ) ;
$ err = RRDs:: error ;
2017-08-28 16:50:15 +01:00
push ( @ output , "ERROR: while graphing $IMG_DIR" . "$IMGz[$n3]: $err\n" ) if $ err ;
2013-01-04 08:27:05 +00:00
}
2013-01-29 11:45:33 +00:00
if ( $ title || ( $ silent =~ /imagetag/ && $ graph =~ /port$n3/ ) ) {
2013-01-04 08:27:05 +00:00
if ( lc ( $ config - > { enable_zoom } ) eq "y" ) {
if ( lc ( $ config - > { disable_javascript_void } ) eq "y" ) {
2017-08-28 16:50:15 +01:00
push ( @ output , " <a href=\"" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ IMGz [ $ n3 ] . "\"><img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ IMG [ $ n3 ] . "' border='0'></a>\n" ) ;
2015-02-12 15:27:03 +00:00
} else {
2015-01-21 16:33:21 +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 ;
}
2017-08-28 16:50:15 +01:00
push ( @ output , " <a href=\"javascript:void(window.open('" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ IMGz [ $ n3 ] . "','','width=" . $ picz_width . ",height=" . $ picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ IMG [ $ n3 ] . "' border='0'></a>\n" ) ;
2013-01-04 08:27:05 +00:00
}
} else {
2017-08-28 16:50:15 +01:00
push ( @ output , " <img src='" . $ config - > { url } . "/" . $ config - > { imgs_dir } . $ IMG [ $ n3 ] . "'>\n" ) ;
2013-01-04 08:27:05 +00:00
}
}
if ( $ title ) {
2017-08-28 16:50:15 +01:00
push ( @ output , " </td>\n" ) ;
2013-01-04 08:27:05 +00:00
}
2013-01-29 11:45:33 +00:00
if ( $ n2 < $ port - > { graphs_per_row } && $ n2 < $ max ) {
$ n2 + + ;
} else {
if ( $ title ) {
2017-08-28 16:50:15 +01:00
push ( @ output , " </tr>\n" ) ;
2013-01-29 11:45:33 +00:00
}
$ n2 = 1 ;
}
$ n3 + + ;
2013-01-04 08:27:05 +00:00
}
2013-01-29 11:45:33 +00:00
$ n + + ;
2013-01-04 08:27:05 +00:00
}
if ( $ title ) {
2017-08-28 16:50:15 +01:00
push ( @ output , main:: graph_footer ( ) ) ;
2013-01-04 08:27:05 +00:00
}
2017-08-28 16:50:15 +01:00
push ( @ output , " <br>\n" ) ;
return @ output ;
2013-01-04 08:27:05 +00:00
}
1 ;