-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcron.php
More file actions
105 lines (95 loc) · 3.97 KB
/
cron.php
File metadata and controls
105 lines (95 loc) · 3.97 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
<?php
// Should be run once a hour
include 'config.php';
function is_changed_pair($ip_address, $mac_address, $vlan_tag) {
global $sql, $DB;
$sth = $sql->prepare('SELECT * FROM known_pairs WHERE ip_address = :ip_address AND vlan_tag = :vlan_tag');
$sth->bindvalue(':ip_address', $ip_address, PDO::PARAM_STR);
$sth->bindvalue(':vlan_tag', $vlan_tag, PDO::PARAM_STR);
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row) {
if($row['mac_address'] == $mac_address && $row['ip_address'] == $ip_address && $row['vlan_tag'] == $vlan_tag) continue;
return 1;
}
return 0;
}
function is_new_pair($ip_address, $mac_address, $vlan_tag) {
global $sql;
$sth = $sql->prepare("SELECT COUNT(ip_address) as count FROM known_pairs WHERE ip_address = :ip_address AND vlan_tag = :vlan_tag");
$sth->bindvalue(":ip_address", $ip_address);
$sth->bindvalue(":vlan_tag", $vlan_tag);
$sth->execute();
if($sth==false) return 1;
$row = $sth->fetch(PDO::FETCH_ASSOC);
if($row['count']==0) return 1;
return 0;
}
function notify_email($type, $pair) {
global $sql, $DB, $CFG;
if(isset($pair['ip_address'])) {
$hostname = gethostbyaddr($pair['ip_address']);
$output = <<<EOF
hostname: $hostname
ip_address: ${pair['ip_address']}
interface: ${pair['interface']}
vlan_tag: ${pair['vlan_tag']}
mac_address: ${pair['mac_address']}
timestamp: ${pair['tstamp']}
History:
EOF;
}
if($type == 'new_pairs') {
$output = "timestamp ip_address mac_address vlan_tag hostname\n";
foreach($pair as $row) {
if(isset($previous) && $previous['mac_address'] == $row['mac_address']) continue;
$hostname = gethostbyaddr($row['ip_address']);
$output .= "${row['tstamp']} ${row['ip_address']} ${row['mac_address']} ${row['vlan_tag']} $hostname\n";
$previous = $row;
}
$subject = "addrwatch: new ip-mac pairs";
} elseif($type == 'changed') {
$sth = $sql->prepare("SELECT * FROM ".$DB['table']." WHERE ip_address = :ip_address ORDER by tstamp DESC");
$sth->bindvalue(":ip_address", $pair['ip_address']);
$sth->execute();
$output .= "timestamp ip_address mac_address vlan_tag\n";
foreach($sth->fetchAll(PDO::FETCH_ASSOC) as $row) {
if(isset($previous) && $previous['mac_address'] == $row['mac_address']) continue;
$output .= "${row['tstamp']} ${row['ip_address']} ${row['mac_address']} ${row['vlan_tag']}\n";
$previous = $row;
}
$subject = "addrwatch: mac_address changed for ip_address ".$pair['ip_address']." (".$hostname.")";
} else {
$subject = "addrwatch: new ip-mac pair ".$pair['ip_address'];
}
if(!mail($CFG['mailto'], $subject, $output)) echo "cant send notify_email\n";
}
$checked_ips = '';
$new_pairs = '';
$result = $sql->query("SELECT * FROM ".$DB['table']." WHERE tstamp > NOW() - INTERVAL 1 HOUR ORDER by tstamp DESC");
foreach($result as $row) {
if(in_array($row['mac_address'], $CFG['mac_ignore'])) continue;
if(isset($checked_ips[$row['ip_address']])) continue;
if(is_changed_pair($row['ip_address'], $row['mac_address'], $row['vlan_tag'])) {
$sth = $sql->prepare('UPDATE known_pairs SET mac_address = :mac_address, changes = changes+1 WHERE vlan_tag = :vlan_tag AND ip_address = :ip_address');
$sth->bindvalue(':ip_address', $row['ip_address'], PDO::PARAM_STR);
$sth->bindvalue(':mac_address', $row['mac_address'], PDO::PARAM_STR);
$sth->bindvalue(':vlan_tag', $row['vlan_tag'], PDO::PARAM_STR);
$sth->execute();
notify_email('changed', $row);
}
if(is_new_pair($row['ip_address'], $row['mac_address'], $row['vlan_tag'])) {
$sth = $sql->prepare("INSERT INTO known_pairs (ip_address,mac_address,vlan_tag,last_change) VALUES (:ip_address,:mac_address,:vlan_tag,NOW())");
$sth->bindvalue(':ip_address', $row['ip_address'], PDO::PARAM_STR);
$sth->bindvalue(':mac_address', $row['mac_address'], PDO::PARAM_STR);
$sth->bindvalue(':vlan_tag', $row['vlan_tag'], PDO::PARAM_STR);
$sth->execute();
//notify_email('new', $row);
$new_pairs[]=$row;
}
$checked_ips[$row['ip_address']]=1;
}
if(!empty($new_pairs)) {
notify_email('new_pairs', $new_pairs);
}
?>