Project

General

Profile

Feature #7671 » softdiscomon.txt

Bridgetowermedia IT, 12/15/2017 03:23 PM

 
1
/*
2
 * softdiscomon
3
 * 
4
 * Soft Disconnect Monitor
5
 * Written by Ansen Labardee and Kris Linnel
6
 *
7
 * This script uses curl to monitor gateways based on the html they recive back from specified sites
8
 * If a specified string is found the html response a specified number of times, we mark the gateway up
9
 * If the string is not found in the page source we mark the gateway as down. 
10
 *
11
 * This script is intened to be run by crontab and should be stored at /etc/phpshellsessions/softdiscomon
12
 * This script can be invoked by the root user with the command "pfSsh.php playback softdiscomon
13
 *
14
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25
 */
26

    
27
require_once("config.inc");
28
require_once("functions.inc");
29

    
30
global $g, $config, $test, $threshhold;
31

    
32

    
33
// Set the URLs, match string and match threshold here.
34
// Threshold is the number of times a match must be found in the html from the configured URL
35

    
36
$threshhold = 3;
37
$test = array();
38

    
39
$test[1]['url'] = "https://google.com/";
40
$test[2]['url'] = "https://www.freebsd.org/";
41
$test[3]['url'] = "https://www.linux.org/";
42

    
43
$test[1]['match'] = 'google.com';
44
$test[2]['match'] = 'freebsd.org';
45
$test[3]['match'] = 'linux.org';
46

    
47

    
48
// Get the friendly interface name of both gateways, if a gateway doesn't exist a NULL value will be returned
49

    
50
$gw0_friendly = $config['gateways']['gateway_item']['0']['interface'];
51
//mai("GW0 Friendly Interface Name: $gw0_friendly \n");
52
$gw1_friendly = $config['gateways']['gateway_item']['1']['interface'];
53
//echo("GW1 Friendly Interface Name: $gw1_friendly \n");
54

    
55
// If the friendly interface name exists then use it to get the real interface name
56
// Also get the current status of the gateway 
57

    
58
if ($gw0_friendly) {
59
  $gw0_interface = $config['interfaces'][$gw0_friendly]['if'];
60
  $gw0_name = $config['gateways']['gateway_item']['0']['name'];
61
//  echo("GW0 Real Interface: $gw0_interface \n");
62
//    echo("GW0 Name: $gw0_name \n");
63
	  $gw0_status = get_dpinger_status($gw0_name)['status'];
64
//	  echo("GW0 Status: $gw0_status \n");
65
}
66

    
67
if ($gw1_friendly) {
68
  $gw1_interface = $config['interfaces'][$gw1_friendly]['if'];
69
  $gw1_name = $config['gateways']['gateway_item']['1']['name'];
70
//  echo("GW1 Real Interface: $gw1_interface \n");
71
//    echo("GW1 Name: $gw1_name \n");
72
	  $gw1_status = get_dpinger_status($gw1_name)['status'];
73
//	  echo("GW1 Status: $gw1_status \n");
74
}
75

    
76
// If the Gateway has a real interface and the status is not down, then begin testing
77
// using the test array and threshholds configured above.
78
// We will test Gateway 0 with URLS 1&2 and Gateway 1 with URLS 2&3, if either urls return sucessfull then Mark
79
// the gateway up, if both urls fail then mark it as down.
80

    
81
if (($gw0_interface) && (!$gw0_status == 'down')) {
82
//		echo("Starting tests on first gateway \n");
83
		$pass_gw0 = 0;
84

    
85
		$pass_gw0 = pass_gw0 + test_url(1, $gw0_interface);
86
		$pass_gw0 = pass_gw0 + test_url(2, $gw0_interface);
87

    
88
		if ($pass_gw0 > 0) {
89
				mark_gw_online(0);
90
		}
91
		else {
92
				mark_gw_offline(0);
93
		}
94
}
95

    
96
if (($gw1_interface) && (!$gw1_status == 'down')) {
97
//		echo("Starting tests on second gateway \n");
98
		$pass_gw1 = 0;
99

    
100
		$pass_gw1 = pass_gw1 + test_url(2, $gw1_interface);
101
		$pass_gw1 = pass_gw1 + test_url(3, $gw1_interface);
102

    
103
		if ($pass_gw1 > 0) {
104
				mark_gw_online(1);
105
		}
106
		else {
107
				mark_gw_offline(1);
108
		}
109
}
110

    
111
exit(0);
112

    
113

    
114
function mark_gw_online($gw_number) {
115
		global $g, $config, $test, $threshhold;
116
		if (!isset($config['gateways']['gateway_item'][$gw_number]['force_down'])) {
117
//			echo("Gateway Already Enabled! \n");
118
				return 0;
119
		}
120
		else {
121
			unset($config['gateways']['gateway_item'][$gw_number]['force_down']);
122
			write_config();
123
				$name = $config['gateways']['gateway_item'][$gw_number]['name'];
124
			exec("/usr/local/bin/mail.php Gateway, $name is back online.");
125
//				echo("Gateway Has Been Enabled! \n");
126
		}
127

    
128
}
129

    
130
function mark_gw_offline($gw_number) {
131
		global $g, $config, $test, $threshhold;
132
		if (isset($config['gateways']['gateway_item'][$gw_number]['force_down'])) {
133
//			echo("Gateway Already Disabled! \n");
134
				return 0;
135
		}
136
		else {
137
			$config['gateways']['gateway_item'][$gw_number]['force_down'] = "";
138
			write_config();
139
				$name = $config['gateways']['gateway_item'][$gw_number]['name'];
140
			exec("/usr/local/bin/mail.php Disabled gateway $name! It seems that our tests have failed.");
141
//				echo("Gateway Has Been Disabled! \n");
142
		}
143

    
144
}
145

    
146
function test_url($test_number, $interface) {
147
		global $g, $config, $test, $threshhold;
148
		$url = $test[$test_number]['url'];
149
		$curl = curl_init($url);
150
		curl_setopt($curl, CURLOPT_INTERFACE, $interface);
151
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
152
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,5);
153
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
154
		$page_source = curl_exec($curl);
155
		curl_close($curl);
156
		
157
		$match = $test[$test_number]['match'];
158
		$count = preg_match_all($match, $page_source);
159
		if ($count >= $threshold) {
160
				return 1;
161
		}
162

    
163
		return 0;
164
}
(1-1/2)