Repository navigation
Expand file tree
/
Copy pathswvpn.pl
More file actions
171 lines (132 loc) · 4.05 KB
/
Copy pathswvpn.pl
File metadata and controls
171 lines (132 loc) · 4.05 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/perl
# -*- coding: utf-8 -*-
#===================================================
#
# Switch entre el enlace de Movistar y conexión de internet para la VPN.
#
# Copyleft (C) 2012 harold Luzardo <haroldburtonl@gmial.com>
#
# This script 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 3 of the License, or (at your option) any later
# version.
#
# This script 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 PACKAGE (see COPYING); if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Created on: Abril 08, 2013 - Harold Luzardo
#
#===================================================
use strict;
use warnings;
use FileHandle;
use Time::Piece;
use LWP::UserAgent;
use Net::Ping;
use Net::Twitter::Lite;
#my $fileLoc = "/var/log/state.txt";
my $archLog = "/var/log/estadoRouters.txt";
#my @hosts = ( "172.16.2.240", "172.18.1.254", "172.18.3.254", "172.18.4.254", "172.18.5.254", "172.18.6.254" );
my @routers = ( "172.16.2.240", "172.18.1.254", "172.18.3.254", "172.18.4.254", "172.18.5.254", "172.18.6.254" );
#my @hostStates = ();
my @EstatusRouters = ();
#my $fh = FileHandle->new("< $archLog");
my $fh = FileHandle->new("< $fileLoc");
while ( <$fh> ) {
# my( $line ) = $_;
my( $posicion ) = $_;
# chomp( $line );
chomp( $posicion );
# push ( @hostStates, $line );
push ( @EstatusRouters, $posicion );
}
$fh->close;
#my $fh = FileHandle->new("> $fileLoc");
my $fh = FileHandle->new("> $archLog");
$fh->close;
foreach my $router ( @routers ) {
my $previousState = getPreviousState( $host );
my $url = "http://$host";
my $ua = LWP::UserAgent->new;
my $response = $ua->get( $url );
my $currentState = $response->code;
my $time = localtime;
# If states are not equal we need to notify someone
if ( $previousState ne $currentState ) {
# Do we have an status code?
if ( $response->code ) {
reportError("$host reports
$response->message.\n");
} else {
# HTTP is not responding,
# is it the network connection down?
my $p = Net::Ping->new("icmp");
if ( $p->ping( $host, 2 )) {
reportError ( "$host is responding,
but Apache is not.\n" );
} else {
reportError ( "$host is unreachable.\n" );
}
}
# Not done yet, we need to log
# the current state for future use
logState( $host, $currentState, $time )
}
sub reportError ($) {
my ( $msg ) = @_;
my $nt = Net::Twitter::Lite->new(
username => $username,
password => $pasword );
my $result = eval { $nt->update( $msg ) };
if ( !$result ) {
# Twitter has failed us,
# need to get the word out still...
smsEmail ( $msg );
}
}
sub smsEmail ($) {
my ( $msg ) = @_;
my $to = "7735551234\@txt.exaple.org";
my $from = "pdw\@weinstein.org";
my $subject = "Service Notification";
my $sendmail = '/usr/lib/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n\n";
print MAIL $msg;
close( MAIL );
}
sub logState ($$$) {
my ( $host, $state, $time ) = @_;
# Create a filehandle on our log file
my $fh = FileHandle->new(">> $fileLoc");
if (defined $fh) {
# Print to file the necessary information,
# delimited with a colon
print $fh "$host:$state:" .$time->datetime. "\n";
$fh->close;
}
}
#sub getPreviousState ($) {
sub EstusPrevRouter ($) {
# my ( $host ) = @_;
my ( $router ) = @_;
# foreach ( @hostStates ) {
foreach ( @EstatusRouteres ) {
# my( $line ) = $_;
my( $posicion ) = $_;
# my ($domain, $state, $time) = split(/:/, $line, 3);
my ($router, $estatus, $hora) = split(/:/, $posicion, 3);
# if ( $domain eq $host ) {
if ( $domain eq $host ) {
return $estatus;
}
}
}