]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/wio/wio.pl
3 ###############################################################################
5 # IPFire.org - A linux based firewall #
6 # Copyright (C) 2017-2020 Stephan Feddersen <sfeddersen@ipfire.org> #
7 # All Rights Reserved. #
9 # This program is free software: you can redistribute it and/or modify #
10 # it under the terms of the GNU General Public License as published by #
11 # the Free Software Foundation, either version 3 of the License, or #
12 # (at your option) any later version. #
14 # This program is distributed in the hope that it will be useful, #
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
17 # GNU General Public License for more details. #
19 # You should have received a copy of the GNU General Public License #
20 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
22 ###############################################################################
24 # Version: 2020/06/01 13:29:23
26 # This wio.pl is based on the code from the IPCop WIO Addon
27 # and is extremly adapted to work with IPFire.
29 # Autor: Stephan Feddersen
30 # Co-Autor: Alexander Marx
31 # Co-Autor: Frank Mainz (for some code for the IPCop WIO Addon)
34 # enable only the following on debugging purpose
38 use POSIX
qw(strftime);
39 use Time
::HiRes
qw(gettimeofday tv_interval);
44 require '/var/ipfire/general-functions.pl';
45 require '/var/ipfire/lang.pl';
46 require '/usr/lib/wio/wio-lib.pl';
48 my ( $debug, $i, $t, $ib, $tb, $ivpn, $tvpn ) = '';
49 my $owner = getpwnam "nobody";
50 my $group = getgrnam "nobody";
51 my $ipadrfile = "/var/log/wio/wioips";
53 unless ( -e
$ipadrfile ) { print ( "The file $ipadrfile doesn't exist!\n" ); exit; }
56 if ( $_ eq '-d' || $_ eq '--debug') { $debug = 1; }
57 if ( $_ eq '-h' || $_ eq '--help' ) { die help
(); }
60 my ( %wiosettings, %mainsettings, %mailsettings, %netsettings ) = ();
62 &General
::readhash
('/var/ipfire/main/settings', \
%mainsettings);
63 &General
::readhash
('/var/ipfire/ethernet/settings', \
%netsettings);
64 &General
::readhash
('/var/ipfire/dma/mail.conf', \
%mailsettings);
65 &General
::readhash
("/var/ipfire/wio/wio.conf", \
%wiosettings);
67 my $now = strftime
"%a, %d.%m.%Y %H:%M:%S", localtime;
68 my $logging = $wiosettings{'LOGGING'};
69 my $mailstyle = $wiosettings{'MAILSTYLE'};
70 my $mailremark = $wiosettings{'MAILREMARK'};
71 my $timeout = $wiosettings{'TIMEOUT'};
72 my $rrddir = "/var/log/rrd/wio";
73 my $onoffip = "/var/log/wio/wioscip";
74 my $hostname = "$mainsettings{'HOSTNAME'}.$mainsettings{'DOMAINNAME'}";
75 my $redactive = "/var/ipfire/red/active";
76 my $rediface = "/var/ipfire/red/iface";
80 $reddev = &General
::get_red_interface
;
83 my $redip = $hostname;
84 my $vpnpid = ( -e
"/var/run/charon.pid" ?
`awk '{print $1}' /var/run/charon.pid`: '');
85 my $ovpnpid = ( -e
"/var/run/openvpn.pid" ?
`awk '{print $1}' /var/run/openvpn.pid`: '');
87 my $steptime = $wiosettings{'CRON'} *= 60;
93 my ( $togglestat, $arp, $time, $start, $timestamp ) = 0;
94 my ( $id, $ipadr, $ipadrnew, $host, $hostnew, $enable, $remark, $dyndns, $dyndnsip ) = '';
95 my ( $mail, $mailon, $mailoff, $ping, $on, $httphost, $mailen ) = '';
96 my ( $msg, $logmsg, $mailmsg, $smailtxt, $infomsg, $client, $mode, $onbak, $arpclient ) = '';
97 my ( $ping_i, $ping_t, $ping_ib, $ping_tb, $ping_iv, $ping_tv, $pingmode ) = '';
98 my ( @tmp, @arptmp, @myarray, @status, @arpclients ) = '';
99 my @ifaces = ('GREEN','BLUE','ORANGE');
102 if ( $mailsettings{'USEMAIL'} eq 'on' ) { $mailen = 'on'; }
103 else { $mailen = 'off'; }
105 if ( -e
$redactive ) {
106 open(IPADDR
, "/var/ipfire/red/local-ipaddress");
113 $start = [gettimeofday
];
118 if ( $netsettings{"${_}_DEV"} ne '' && $netsettings{"${_}_DEV"} ne 'disabled' ) {
119 my $output = `ifconfig $netsettings{"${_}_DEV"}`;
121 if ( grep (/RX bytes:0/, $output) ) { next; }
123 @arptmp = `/usr/local/bin/wioscan -s $netsettings{"${_}_DEV"}`;
125 foreach $arpclient (@arptmp) {
126 push (@arpclients, (split (/\,/,$arpclient))[1]);
134 if ( -e
"$onoffip" ) { open( FILE
, "< $onoffip" ); }
135 else { open( FILE
, "< $ipadrfile" ); }
143 @tmp = split( /\,/, $_ );
145 ($id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost) = @tmp;
147 $timestamp = strftime
"%d.%m.%Y - %H:%M:%S", localtime;
149 if ( $enable ne 'on' ) {
150 push (@status, "$id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost\n");
154 if ( defined($dyndns) && ( $dyndns =~ 'on' ) ) {
155 ($dyndnsip, $infomsg) = &WIO
::getdyndnsip
($host, @myarray);
156 if ($dyndnsip ne $ipadr) { $ipadr = $dyndnsip; }
159 $ping_i = $ping_t = $ping_ib = $ping_tb = $ping_iv = $ping_tv = $pingmode = $arp = 0;
161 foreach (@arpclients) {
163 unless ( $ipadr eq $_ )
165 $i = Net
::Ping
->new( $i_ping, $timeout );
166 unless ( defined $i ) { die "Can't create Net::Ping object $!"; }
168 $t = Net
::Ping
->new( $t_ping, $timeout );
169 unless ( defined $t ) { die "Can't create Net::Ping object $!"; }
171 $ib = Net
::Ping
->new( $i_ping, $timeout );
172 unless ( defined $ib ) { die "Can't create Net::Ping object $!"; }
175 $tb = Net
::Ping
->new( $t_ping, $timeout );
176 unless ( defined $tb ) { die "Can't create Net::Ping object $!"; }
179 if ($ovpnpid || $vpnpid)
181 $ivpn = Net
::Ping
->new( $i_ping, $timeout );
182 unless ( defined $ivpn ) { die "Can't create Net::Ping object $!"; }
183 $ivpn->bind($hostname);
185 $tvpn = Net
::Ping
->new( $t_ping, $timeout );
186 unless ( defined $tvpn ) { die "Can't create Net::Ping object $!"; }
187 $tvpn->bind($hostname);
193 $client = ( ( $dyndns eq 'on' || $ping eq 'fqdn' ) ?
$host : $ipadr );
196 printf "%2s %15s", $nr++, ($client ne $ipadr ?
$ipadr : $client );
197 $time = [gettimeofday
];
201 || ($ping_i = $i->ping($client))
202 || ($ping_t = $t->ping($client))
203 || ($ping_ib = $ib->ping($client))
204 || ($ping_tb = $tb->ping($client))
205 || ($ovpnpid?
($ping_iv = $ivpn->ping($client)) : 0)
206 || ($vpnpid?
($ping_tv = $tvpn->ping($client)) : 0) )
209 $msg = "$Lang::tr{'wio up'}";
211 $togglestat = ( $on ne 'on' ) ?
1 : 0;
216 $msg = "$Lang::tr{'wio down'}";
218 $togglestat = ( $on ne 'off' ) ?
1 : 0;
222 push (@status, "$id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost\n");
226 if ( $mailon eq 'on' && $togglestat == 1 && $mode == 100 ) { $mail = 'Online'; }
227 if ( $mailoff eq 'on' && $togglestat == 1 && $mode == 0 ) { $mail = 'Offline'; }
228 if ( $dyndns ne 'on' ) { $dyndns = 'off'; }
230 $pingmode = $arp ?
'ARPSCAN' : $ping_i ?
'ICMP' : $ping_t ?
'TCP' : $ping_ib ?
'ICMP+BIND' : $ping_tb ?
'TCP+BIND' : $ping_iv ?
'VPN ICMP' : $ping_tv ?
'VPN TCP' : 'NO ECHO';
231 printf "%7s%8s%9s%10s %.4f sek%12s\n", $ping, $dyndns, $msg, $mail, tv_interval
($time), $pingmode;
234 if ( $host eq '' ) { $hostnew = 'n/a'; } else { $hostnew = $host; }
235 if ( $ipadr eq '' ) { $ipadrnew = 'n/a'; } else { $ipadrnew = $ipadr; }
237 if ( $logging eq 'on' ) {
238 $logmsg = "Client: $hostnew - IP: $ipadrnew - Status: $msg";
239 &General
::log("wio","$logmsg");
242 if ( $mailen eq 'on' && $togglestat == 1 && ($mailon eq 'on' || $mailoff eq 'on')) {
244 if ( $mailstyle eq 'email' || ($mailstyle eq 'smail' && $smailtxt eq '') ) { $mailmsg .= "Date\t : $now\n\n"; }
246 $mailmsg .= "Client\t : $hostnew\nIP\t : $ipadrnew\nStatus\t : $msg\n";
248 if ( $mailremark eq 'on' && $remark ne '' ) {
249 $mailmsg .= "Remark : $remark\n\n";
252 if ( $mailstyle eq 'email' ) {
253 &WIO
::mailsender
("WIO - $host - $msg", $mailmsg);
256 elsif ( $mailstyle eq 'smail' ) {
257 $smailtxt .= $mailmsg."\n";
262 if ( $ping ne 'fqdn' ) { $client = $host; }
263 if ( $host eq '' ) { $client = $ipadr; }
265 updatewiodata
("$id");
274 if ( ( -e
$ovpnpid || -e
$vpnpid ) && $arp == 0 ) {
280 # write ipadressfile new
282 if ( !-e
$onoffip ) {
283 open( FILE
, "> $ipadrfile" );
288 system("/bin/sed -i 's#$tmp[0],$tmp[1],$tmp[2],$tmp[3],$tmp[4],$tmp[5],$tmp[6],$tmp[7],$tmp[8],$tmp[9],$tmp[10],$tmp[11]#$id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost#g' $ipadrfile");
289 chmod ( 0644, $ipadrfile );
290 chown ( $owner, $group, $ipadrfile );
294 printf ("\n$Lang::tr{'wio_scriptruntime'}: %.4f $Lang::tr{'age ssecond'}\n\n", tv_interval
($start));
297 if ( $smailtxt ne '' ) { &WIO
::mailsender
($Lang::tr
{'wio_sub'}, $smailtxt); }
305 if ( -e
$onoffip ) { unlink($onoffip); }
310 if ( !-e
"$rrddir/$id.rrd" ) {
312 "$rrddir/$id.rrd", "--step=$steptime",
313 "DS:mode:GAUGE:3600:0:100", "RRA:AVERAGE:0.5:1:576",
314 "RRA:AVERAGE:0.5:6:672", "RRA:AVERAGE:0.5:24:732",
315 "RRA:AVERAGE:0.5:144:1460"
317 my $ERROR = RRDs
::error
;
318 print "Error in RRD::create for Who Is Online: $ERROR\n" if $ERROR;
321 RRDs
::update
( "$rrddir/$id.rrd", "-t", "mode", "N:$mode" );
323 my $error = RRDs
::error
;
325 if ($error) { &General
::log("wio","$error"); }
331 TIMEOUT : $timeout $Lang::tr{'age ssecond'}
332 MAILSTYLE : $mailstyle
333 RED TYPE : $netsettings{'RED_TYPE'}
338 if ($ovpnpid) {printf "OpenVPN PID : $ovpnpid"}
339 if ($vpnpid) {printf "IPsec PID : $vpnpid"}
342 $Lang::tr{'wio_search'}
344 %3s%17s%7s%8s%9s%10s%15s%12s
345 ---------------------------------------------------------------------------------
346 ","ID ", "$Lang::tr{'wio ipadress'}", "Ping", "DynDNS", "Status", "Mail", "$Lang::tr{'wio_answer_time'}", "$Lang::tr{'wio_answer'}";
351 Who Is Online? for IPFire
353 use option -d for debugging
354 use option -h for help\n\n";