-
Notifications
You must be signed in to change notification settings - Fork 1
/
SunMonitor.php
229 lines (211 loc) · 7.08 KB
/
SunMonitor.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* SunMonitor Class
*
* @category Server Monitoring
* @package SunMonitor
* @author Mehmet Selcuk Batal <batalms@gmail.com>
* @copyright Copyright (c) 2024, Sunhill Technology <www.sunhillint.com>
* @license https://opensource.org/licenses/lgpl-3.0.html The GNU Lesser General Public License, version 3.0
* @link https://github.com/msbatal/PHP-Server-Monitoring-Class
* @version 1.3.7
*/
class SunMonitor
{
/**
* Array that holds server ips/domains
* @var array
*/
private $hostList = [];
/**
* Array that holds server names
* @var array
*/
private $nameList = [];
/**
* Array that holds log records
* @var array
*/
private $recordList = [];
/**
* Array that holds server statuses
* @var array
*/
private $serverStatus = [];
/**
* Logging to file (on/off)
* @var boolean
*/
private $fileOutput = false;
/**
* Number of ping attempt
* @var integer
*/
protected $attempt = 2;
/**
* @param string|array $hosts
* @param string|array $names
* @param boolean $output
*/
public function __construct($hosts = null, $names = null, $output = false) {
set_exception_handler(function($exception) {
echo '<b>[SunClass] Exception:</b> ' . $exception->getMessage();
});
if (is_array($hosts)) { // assign ip addresses/domains to the array
$this->hostList = $hosts;
} else { // insert ip address/domain into the array
$this->hostList[] = $hosts;
}
if (is_array($names)) { // assign server names to the array
$this->nameList = $names;
} else { // insert server name into the array
$this->nameList[] = $names;
}
$this->fileOutput = $output;
$this->validate();
return $this;
}
/**
* Validate input values
*
* @throws exception
*/
private function validate() {
if (count($this->hostList) == 0) {
throw new \Exception('Ip Address/Domain is not set.');
}
$this->validateHost(); // ip/domain validation
if (count($this->hostList) != count($this->nameList)) {
$this->validateName(); // server name validation
}
}
/**
* Validate host ips and/or domains
*/
private function validateHost() {
foreach ($this->hostList as $key => $value) {
if (filter_var($value, FILTER_VALIDATE_IP) == true) {
continue;
} else {
if (filter_var($value, FILTER_VALIDATE_DOMAIN) == true) {
$value = preg_replace('/https?:\/\/|www.|\/$/', '', $value); // remove protocol and subdomain
if (substr($value, -1) != '.') {
$value .= '.'; // add dot (.) end of domain
}
$ipAddress = gethostbyname($value); // get ip address from domain
//$dnsA = dns_get_record($value, DNS_A); // get ip address from dns record
//$ipAddress = $dnsA[0]['ip'];
$this->hostList[$key] = $ipAddress;
} else {
unset($this->hostList[$key]);
}
}
}
}
/**
* Validate server names (alias)
*/
private function validateName() {
$empty = count($this->hostList) - count($this->nameList);
for ($i = 1; $i <= $empty; $i++) {
$this->nameList[] = 'Noname Server #' . $i; // assign server name if not set by admin
}
}
/**
* Create log file and save ping results
*
* @throws exception
*/
private function saveLog() {
if ($this->fileOutput != true) {
throw new \Exception('Logging is turned off in settings.');
}
$jsonFile = 'logs/' . date('Ymd') . '.json'; // json file location and name
if (file_exists($jsonFile)) { // update file with new records (if exists)
$content = file_get_contents($jsonFile);
$jsonTemp = json_decode($content, true);
array_push($jsonTemp, $this->serverStatus);
$jsonData = json_encode($jsonTemp);
file_put_contents($jsonFile, $jsonData);
} else { // create file with the records (if not exists)
$jsonData = '[' . json_encode($this->serverStatus) . ']';
$file = fopen($jsonFile, 'a+');
fwrite($file, $jsonData);
fclose($file);
}
}
/**
* Monitor servers and save results to log file
*/
public function monitor() {
$content = [];
$counter = 0;
$this->serverStatus = [];
foreach ($this->hostList as $host) {
$pingreply = exec("ping -c $this->attempt $host", $output, $result); // ping process
if (substr($pingreply, -2) == 'ms' || $result == '0') { // if reach server (successful)
$speed = explode("/", $pingreply);
$speed = $speed[4]." ms";
$status = "Available";
} else { // if can't reach server (unsuccessful)
$speed = "Timeout";
$status = "Unavailable";
}
$this->serverStatus[] = ["datetime" => date("Y-m-d H:i:s"), "ip" => $host, "name" => $this->nameList[$counter], "status" => $status, "speed" => $speed, "reply" => $pingreply]; // ping result
$counter++;
}
if ($this->fileOutput == true) {
$this->saveLog(); // save result to log file
}
return $this;
}
/**
* Print records by reading log file
*
* @param string $date
* @param integer $order
* @throws exception
* @return array
*/
public function getLogs($date = null, $order = 0) {
if (empty($date)) {
if ($this->fileOutput != true) {
throw new \Exception('Logging is turned off in settings.');
}
$date = date('Y-m-d'); // if not set a specific date
}
$date = str_replace('-', '', $date);
$jsonFile = 'logs/' . $date . '.json';
if (!file_exists($jsonFile)) {
throw new \Exception('No such file/directory.');
}
$content = file_get_contents($jsonFile); // read log file content
$jsonData = json_decode($content, true); // convert content to array
if ($order > 0) {
return $jsonData[$order-1]; // return a specific record
} else {
return $jsonData; // return whole records
}
}
/**
* Send server monitoring results (detail)
*
* @return array
*/
public function list() {
return $this->serverStatus; // send whole records with all monitoring details
}
/**
* Send server monitoring result (summary)
*
* @return array
*/
public function result() {
$result = [];
foreach ($this->serverStatus as $status) {
$result[$status['ip']] = $status['status'];
}
return $result; // send whole records with summarized info (ip and status)
}
}
?>