-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmap.php
More file actions
66 lines (66 loc) · 1.88 KB
/
submap.php
File metadata and controls
66 lines (66 loc) · 1.88 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
<?php
function sum_state($filename) {
$file_in = fopen($filename, "r");
if (!$file_in) { exit; }
while (!feof($file_in)) {
$fin_line=rtrim(fgets($file_in, 1024));
if(!strlen($fin_line)) continue; //skip empty lines
strtok($fin_line, ";");
$status=strtok(";");
if($status==0) {
fclose($file_in);
return 0;
}
}
fclose($file_in);
return 1;
}
if ($argc!=2) die("usage: php submap.php datadir\n");
$datadir = $argv[1];
//TODO: add/remove / in the end
$files = scandir($datadir);
$state=array();
foreach ($files as $f) {
if (substr($f,-6)!='submap') continue;
$submap=array();
$file_in = fopen($datadir.$f, "r");
if (!$file_in) { exit; }
while (!feof($file_in)) {
$fin_line=rtrim(fgets($file_in, 1024));
if(!strlen($fin_line)) continue; //skip empty lines
$tmp = strtok($fin_line, ";");
$submap[$tmp]=strtok(";");
$state[$submap[$tmp]]=sum_state($datadir.$submap[$tmp].".state");
}
$file_in = fopen($datadir.substr($f,0,-6)."comp", "r");
if (!$file_in) { exit; }
$ip_map=array();
while (!feof($file_in)) {
$fin_line=rtrim(fgets($file_in, 1024));
if(!strlen($fin_line)) continue; //skip empty lines
$name = strtok($fin_line, ";");
if (!array_key_exists($name,$submap)) continue;
$ip=strtok(";");
$ip_map[$ip]=$name;
}
fclose($file_in);
$cont="";
$file_state = fopen($datadir.substr($f,0,-6)."state", "r");
if (!$file_state) { exit; }
while (!feof($file_state)) {
$fin_line=rtrim(fgets($file_state, 1024));
if(!strlen($fin_line)) continue; //skip empty lines
$ip = strtok($fin_line, ";");
if (array_key_exists($ip,$ip_map)) {
$status=strtok(";");
if ($state!=0) {
if(!$state[$submap[$ip_map[$ip]]]) $status=3;
}
$rtt=strtok(";");
$fin_line="$ip;$status;$rtt";
}
$cont.=$fin_line."\n";
}
file_put_contents($datadir.substr($f,0,-6)."state",$cont);
}
?>