1 |
<?php |
---|
2 |
|
---|
3 |
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 |
|
---|
8 |
|
---|
9 |
|
---|
10 |
|
---|
11 |
|
---|
12 |
|
---|
13 |
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 |
|
---|
18 |
|
---|
19 |
|
---|
20 |
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 |
|
---|
25 |
function init_db() { |
---|
26 |
global $dbh, $db_host, $db_name, $db_user, $db_pass; |
---|
27 |
try { |
---|
28 |
if ($database = 'mysql') { |
---|
29 |
$dbh = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass); |
---|
30 |
} else if ($database = 'postgresql') { |
---|
31 |
$dbh = new PDO('pgsql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass); |
---|
32 |
} |
---|
33 |
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); |
---|
34 |
} catch (PDOException $e) { |
---|
35 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
36 |
die(); |
---|
37 |
} |
---|
38 |
} |
---|
39 |
|
---|
40 |
|
---|
41 |
function get_num_probes() { |
---|
42 |
global $dbh; |
---|
43 |
try { |
---|
44 |
$q = $dbh->query("SELECT COUNT(FlowSrc) FROM FlowSources;"); |
---|
45 |
$r = $q->fetch(); |
---|
46 |
} catch (PDOException $e) { |
---|
47 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
48 |
die(); |
---|
49 |
} |
---|
50 |
|
---|
51 |
return $r[0]; |
---|
52 |
} |
---|
53 |
|
---|
54 |
|
---|
55 |
function get_num_flows() { |
---|
56 |
global $dbh; |
---|
57 |
try { |
---|
58 |
$q = $dbh->query("SELECT COUNT(FlowSrc) FROM Flows;"); |
---|
59 |
$r = $q->fetch(); |
---|
60 |
} catch (PDOException $e) { |
---|
61 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
62 |
die(); |
---|
63 |
} |
---|
64 |
|
---|
65 |
return $r[0]; |
---|
66 |
} |
---|
67 |
|
---|
68 |
|
---|
69 |
function get_num_systems() { |
---|
70 |
global $dbh; |
---|
71 |
try { |
---|
72 |
$q = $dbh->query("SELECT COUNT(DISTINCT IPSrc) FROM Flows;"); |
---|
73 |
$r = $q->fetch(); |
---|
74 |
} catch (PDOException $e) { |
---|
75 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
76 |
die(); |
---|
77 |
} |
---|
78 |
|
---|
79 |
return $r[0]; |
---|
80 |
} |
---|
81 |
|
---|
82 |
|
---|
83 |
function get_oldest_flow_ts() { |
---|
84 |
global $dbh; |
---|
85 |
try { |
---|
86 |
$q = $dbh->query("SELECT MIN(TimeStart) FROM Flows;"); |
---|
87 |
$r = $q->fetch(); |
---|
88 |
} catch (PDOException $e) { |
---|
89 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
90 |
die(); |
---|
91 |
} |
---|
92 |
|
---|
93 |
return $r[0]; |
---|
94 |
} |
---|
95 |
|
---|
96 |
|
---|
97 |
function get_newest_flow_ts() { |
---|
98 |
global $dbh; |
---|
99 |
try { |
---|
100 |
$q = $dbh->query("SELECT MAX(TimeStop) FROM Flows;"); |
---|
101 |
$r = $q->fetch(); |
---|
102 |
} catch (PDOException $e) { |
---|
103 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
104 |
die(); |
---|
105 |
} |
---|
106 |
|
---|
107 |
return $r[0]; |
---|
108 |
} |
---|
109 |
|
---|
110 |
|
---|
111 |
function get_bytes() { |
---|
112 |
global $dbh; |
---|
113 |
try { |
---|
114 |
$q = $dbh->query("SELECT SUM(Bytes) FROM Flows;"); |
---|
115 |
$r = $q->fetch(); |
---|
116 |
} catch (PDOException $e) { |
---|
117 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
118 |
die(); |
---|
119 |
} |
---|
120 |
|
---|
121 |
return $r[0]; |
---|
122 |
} |
---|
123 |
|
---|
124 |
|
---|
125 |
function get_pkts() { |
---|
126 |
global $dbh; |
---|
127 |
try { |
---|
128 |
$q = $dbh->query("SELECT SUM(Pakets) FROM Flows;"); |
---|
129 |
$r = $q->fetch(); |
---|
130 |
} catch (PDOException $e) { |
---|
131 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
132 |
die(); |
---|
133 |
} |
---|
134 |
|
---|
135 |
return $r[0]; |
---|
136 |
} |
---|
137 |
|
---|
138 |
|
---|
139 |
function get_db_size() { |
---|
140 |
global $dbh; |
---|
141 |
try { |
---|
142 |
$size = 0; |
---|
143 |
foreach($dbh->query("SHOW TABLE STATUS") as $row) { |
---|
144 |
$size += $row['Data_length'] + $row['Index_length']; |
---|
145 |
} |
---|
146 |
} catch (PDOException $e) { |
---|
147 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
148 |
die(); |
---|
149 |
} |
---|
150 |
return $size; |
---|
151 |
} |
---|
152 |
|
---|
153 |
|
---|
154 |
function get_probes() { |
---|
155 |
global $dbh; |
---|
156 |
$probes = array(); |
---|
157 |
try { |
---|
158 |
foreach($dbh->query("SELECT FlowSrc, Hostname FROM FlowSources") as $row) { |
---|
159 |
|
---|
160 |
$ip = long2ip($row['FlowSrc']); |
---|
161 |
if (empty($row['Hostname'])) { |
---|
162 |
$row['Hostname'] = gethostbyaddr($ip); |
---|
163 |
} |
---|
164 |
$probes[$ip] = $row['Hostname']; |
---|
165 |
} |
---|
166 |
} catch (PDOException $e) { |
---|
167 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
168 |
die(); |
---|
169 |
} |
---|
170 |
return $probes; |
---|
171 |
} |
---|
172 |
|
---|
173 |
|
---|
174 |
|
---|
175 |
function isvalid_probe($ip) { |
---|
176 |
global $dbh; |
---|
177 |
$probes = array(); |
---|
178 |
try { |
---|
179 |
$q = $dbh->prepare("SELECT FlowSrc FROM FlowSources WHERE FlowSrc = ?;"); |
---|
180 |
$q->bindParam(1, sprintf("%u", ip2long($ip))); |
---|
181 |
$q->execute(); |
---|
182 |
$r = $q->fetch(); |
---|
183 |
} catch (PDOException $e) { |
---|
184 |
print "Error!: " . $e->getMessage() . "<br/>"; |
---|
185 |
die(); |
---|
186 |
} |
---|
187 |
if ($ip == long2ip($r[0])) { |
---|
188 |
return True; |
---|
189 |
} |
---|
190 |
return False; |
---|
191 |
} |
---|
192 |
|
---|
193 |
|
---|
194 |
|
---|
195 |
|
---|