1 | <?php
|
---|
2 |
|
---|
3 | class ConfigCheckPorts extends NetworkConfigItem
|
---|
4 | {
|
---|
5 | function CheckPortStatus(string $IP, int $Port, string $Protocol = 'tcp'): int
|
---|
6 | {
|
---|
7 | $Timeout = 1;
|
---|
8 | $State = 0;
|
---|
9 | if ($Protocol == 'tcp') $Prefix = '';
|
---|
10 | else if ($Protocol == 'udp') $Prefix = 'udp://';
|
---|
11 | else throw new Exception('Unsupported protocol "'.$Protocol.'"');
|
---|
12 | try
|
---|
13 | {
|
---|
14 | $LastErrorReporting = error_reporting();
|
---|
15 | error_reporting(0);
|
---|
16 | if ($Socket = @fsockopen($Prefix.$IP, $Port, $ErrorNumber, $ErrorString, $Timeout))
|
---|
17 | {
|
---|
18 | fclose($Socket);
|
---|
19 | $State = 1;
|
---|
20 | }
|
---|
21 | }
|
---|
22 | finally
|
---|
23 | {
|
---|
24 | error_reporting($LastErrorReporting);
|
---|
25 | }
|
---|
26 | return $State;
|
---|
27 | }
|
---|
28 |
|
---|
29 | function CheckPorts(): void
|
---|
30 | {
|
---|
31 | $StartTime = time();
|
---|
32 |
|
---|
33 | // Load all ports to memory
|
---|
34 | $Ports = array();
|
---|
35 | $DbResult = $this->Database->query('SELECT `NetworkPort`.`Id`, `NetworkPort`.`Number`, `NetworkPort`.`Protocol`, '.
|
---|
36 | '`NetworkPort`.`Online`, 0 AS `NewOnline`, `NetworkInterface`.`LocalIP` AS `LocalIP` '.
|
---|
37 | 'FROM `NetworkPort` '.
|
---|
38 | 'LEFT JOIN `NetworkInterface` ON `NetworkInterface`.`Id`=`NetworkPort`.`Interface` '.
|
---|
39 | 'LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id`=`NetworkInterface`.`Device` '.
|
---|
40 | 'WHERE (`NetworkPort`.`Enabled`=1) AND (`NetworkInterface`.`LocalIP` !="") AND (`NetworkInterface`.`Enabled`=1) AND'.
|
---|
41 | '(`NetworkDevice`.`Used`=1)');
|
---|
42 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
43 | $Ports[$DbRow['Id']] = $DbRow;
|
---|
44 |
|
---|
45 | foreach ($Ports as $Index => $Port)
|
---|
46 | {
|
---|
47 | if ($Port['Protocol'] == 0) $Port['Protocol'] = 'tcp';
|
---|
48 | if ($Port['Protocol'] == 1) $Port['Protocol'] = 'udp';
|
---|
49 | $Port['NewOnline'] = $this->CheckPortStatus($Port['LocalIP'], $Port['Number'], $Port['Protocol']);
|
---|
50 |
|
---|
51 | // Update last online time if still online
|
---|
52 | if ($Port['NewOnline'])
|
---|
53 | {
|
---|
54 | $DbResult = $this->Database->update('NetworkPort', '`Id` = "'.$Port['Id'].'"',
|
---|
55 | array('Online' => 1, 'LastOnline' => TimeToMysqlDateTime($StartTime)));
|
---|
56 | }
|
---|
57 |
|
---|
58 | // Update UpDown table
|
---|
59 | if ($Port['Online'] != $Port['NewOnline'])
|
---|
60 | {
|
---|
61 | // Online state changed
|
---|
62 | $DbResult = $this->Database->query('INSERT INTO `NetworkPortUpDown` (`Port`,
|
---|
63 | `State`, `Time`, `Duration`) VALUES ('.$Port['Id'].', '.$Port['NewOnline'].', "'.
|
---|
64 | TimeToMysqlDateTime($StartTime).'", NULL)');
|
---|
65 | // Update previous record duration in UpDown table
|
---|
66 | $this->Database->query('UPDATE `NetworkPortUpDown` AS `TM` SET `Duration` = TIMESTAMPDIFF(SECOND, '.
|
---|
67 | '`TM`.`Time`, (SELECT `Time` FROM (SELECT * FROM `NetworkPortUpDown`) AS `TA` WHERE (`TA`.`Time` > `TM`.`Time`) '.
|
---|
68 | 'AND (`TA`.`Port`=`TM`.`Port`) ORDER BY `TA`.`Time` ASC LIMIT 1)) '.
|
---|
69 | 'WHERE (`TM`.`Duration` IS NULL) AND (`TM`.`Port` ='.$Port['Id'].')');
|
---|
70 | }
|
---|
71 | }
|
---|
72 | $DbResult = $this->Database->update('NetworkPort', '`LastOnline` < "'.
|
---|
73 | TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
|
---|
74 |
|
---|
75 | // Set offline all ports which were not updated as online
|
---|
76 | $DbResult = $this->Database->select('NetworkPort', '*', '(`Online` = 1) AND '.
|
---|
77 | '(`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'")');
|
---|
78 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
79 | {
|
---|
80 | echo('Port '.$DbRow['Number'].' online but time not updated.'."\n");
|
---|
81 | }
|
---|
82 | $DbResult = $this->Database->select('NetworkPort', '*', '(`Online` = 0) AND '.
|
---|
83 | '(`LastOnline` >= "'.TimeToMysqlDateTime($StartTime).'")');
|
---|
84 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
85 | {
|
---|
86 | echo('Port '.$DbRow['Number'].' not online but time updated.'."\n");
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | function Run(): void
|
---|
91 | {
|
---|
92 | RepeatFunction(60, array($this, 'CheckPorts'));
|
---|
93 | }
|
---|
94 | }
|
---|