-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_nodewatcher.php
286 lines (250 loc) · 13 KB
/
api_nodewatcher.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
require_once('runtime.php');
require_once(ROOT_DIR.'/lib/core/login.class.php');
require_once(ROOT_DIR.'/lib/core/router_old.class.php');
require_once(ROOT_DIR.'/lib/core/routersnotassigned.class.php');
require_once(ROOT_DIR.'/lib/core/rrdtool.class.php');
require_once(ROOT_DIR.'/lib/core/interfaces.class.php');
require_once(ROOT_DIR.'/lib/core/crawling.class.php');
require_once(ROOT_DIR.'/lib/extern/phpass/PasswordHash.php');
require_once(ROOT_DIR.'/lib/core/RouterStatus.class.php');
require_once(ROOT_DIR.'/lib/core/Networkinterface.class.php');
require_once(ROOT_DIR.'/lib/core/NetworkinterfaceStatus.class.php');
require_once(ROOT_DIR.'/lib/core/Validation.class.php');
if($_GET['section']=="get_standart_data") {
if ($_GET['authentificationmethod']=='hash') {
$router_data = Router_old::getRouterByAutoAssignHash($_GET['router_auto_update_hash']);
}
if(!empty($router_data)) {
echo "success;".$router_data['router_id'].";".$router_data['router_auto_assign_hash'].";".$router_data['hostname'];
} else {
echo "error;router_not_found";
}
}
if($_GET['section']=="test_login_strings") {
$login_strings = explode(";", $_GET['login_strings']);
$exist=false;
foreach($login_strings as $login_string) {
if(!empty($login_string)) {
$router_data = Router_old::getRouterByAutoAssignLoginString($login_string);
if(!empty($router_data)) {
$exist=true;
echo "success;$login_string";
break;
}
}
}
if(!$exist) {
echo "error;login_string_not_found";
}
}
if($_GET['section']=="router_auto_assign") {
$router_data = Router_old::getRouterByAutoAssignLoginString($_GET['router_auto_assign_login_string']);
if(empty($router_data)) {
$router = RoutersNotAssigned::getRouterByAutoAssignLoginString($_GET['router_auto_assign_login_string']);
if (empty($router)) {
//Make DB Insert
try {
$stmt = DB::getInstance()->prepare("INSERT INTO routers_not_assigned (create_date, update_date, hostname, router_auto_assign_login_string, interface)
VALUES (NOW(), NOW(), :hostname, :router_auto_assign_login_string, :interface);");
$stmt->execute(array(
':hostname' => $_GET['hostname'],
':router_auto_assign_login_string' => $_GET['router_auto_assign_login_string'],
':interface' => $_GET['interface']
));
}
catch(PDOException $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
}
echo "error;new_not_assigned;;$_GET[router_auto_assign_login_string]";
} else {
try {
$result = DB::getInstance()->exec("UPDATE routers_not_assigned SET
update_date = NOW()
WHERE id = '$router[id]'");
}
catch(PDOException $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
}
echo "error;updated_not_assigned;;$_GET[router_auto_assign_login_string]";
}
} elseif ($router_data['allow_router_auto_assign']==0) {
echo "error;autoassign_not_allowed;$_GET[router_auto_assign_login_string]";
} elseif(!empty($router_data['router_auto_assign_hash'])) {
echo "error;already_assigned;$_GET[router_auto_assign_login_string]";
} else {
//generate random string
$hash = md5(
uniqid(
(string)microtime(true)
+sha1(
(string)rand(0,10000) //100% Zufall
+$thumb_tmp_name
)
)
+md5($orig_name)
);
//Save hash to DB
$result = DB::getInstance()->exec("UPDATE routers SET
router_auto_assign_hash = '$hash'
WHERE id = '$router_data[router_id]'");
//Make output
echo "success;".$router_data['router_id'].";".$hash.";".$router_data['hostname'];
}
}
if($_GET['section']=="get_hostnames_and_mac") {
$last_endet_crawl_cycle = Crawling::getLastEndedCrawlCycle();
//echo "<pre>";
$interfaces = array();
try {
$sql = "SELECT DISTINCT crawl_batman_advanced_interfaces.name, routers.hostname, crawl_interfaces.mac_addr
FROM crawl_batman_advanced_interfaces, routers, crawl_interfaces
WHERE crawl_batman_advanced_interfaces.crawl_cycle_id='$last_endet_crawl_cycle[id]' AND
routers.id=crawl_batman_advanced_interfaces.router_id AND
crawl_interfaces.crawl_cycle_id='$last_endet_crawl_cycle[id]' AND
crawl_interfaces.router_id=crawl_batman_advanced_interfaces.router_id AND
crawl_interfaces.name LIKE crawl_batman_advanced_interfaces.name
GROUP BY crawl_interfaces.mac_addr";
$result = DB::getInstance()->query($sql);
foreach($result as $row) {
echo $row['mac_addr']." ".$row['hostname']."_".$row['name']."\n";
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
/** Nodewatcher Version >18 */
if($_GET['section']=="insert_crawl_data") {
is_int($_POST['router_id']) or die('Invalid router id');
$router_data = Router_old::getRouterInfo($_POST['router_id']);
//If is owning user or if root
if((($_POST['authentificationmethod']=='hash') AND ($router_data['allow_router_auto_assign']==1 AND !empty($router_data['router_auto_assign_hash']) AND $router_data['router_auto_assign_hash']==$_POST['router_auto_update_hash']))) {
echo "success;".$router_data['hostname'].";";
$actual_crawl_cycle = Crawling::getActualCrawlCycle();
$router_has_been_crawled = Crawling::checkIfRouterHasBeenCrawled($_POST['router_id'], $actual_crawl_cycle['id']);
if(!$router_has_been_crawled) {
$last_endet_crawl_cycle = Crawling::getLastEndedCrawlCycle();
/**Insert Router System Data*/
$router_status = New RouterStatus(false, (int)$actual_crawl_cycle['id'], (int)$_POST['router_id'],
$_POST['status'], false, $_POST['hostname'], (int)$_POST['client_count'], $_POST['chipset'],
$_POST['cpu'], (int)$_POST['memory_total'], (int)$_POST['memory_caching'], (int)$_POST['memory_buffering'],
(int)$_POST['memory_free'], $_POST['loadavg'], $_POST['processes'], $_POST['uptime'],
$_POST['idletime'], $_POST['local_time'], $_POST['distname'], $_POST['distversion'], $_POST['openwrt_core_revision'],
$_POST['openwrt_feeds_packages_revision'], $_POST['firmware_version'],
$_POST['firmware_revision'], $_POST['kernel_version'], $_POST['configurator_version'],
$_POST['nodewatcher_version'], $_POST['fastd_version'], $_POST['batman_advanced_version']);
$router_status->store();
/**Insert Router Interfaces*/
foreach($_POST['int'] as $sendet_interface) {
if (!Validation::isValidInterfaceName($sendet_interface['name'])) {
echo 'Invalid interface name!';
continue;
}
/**
* Interface
*/
//check if interface already exists
$networkinterface_test = new Networkinterface(false, (int)$_POST['router_id'], $sendet_interface['name']);
//if interface not exist, create new
if(!$networkinterface_test->fetch()) {
$networkinterface_new = new Networkinterface(false, (int)$_POST['router_id'], $sendet_interface['name']);
$networkinterface_id = $networkinterface_new->store();
} else {
$networkinterface_id = $networkinterface_test->getNetworkinterfaceId();
}
//save crawl data for interface
$networkinterface_status = new NetworkinterfaceStatus(false, false, (int)$networkinterface_id, (int)$_POST[router_id],
$sendet_interface['name'], $sendet_interface['mac_addr'], (int)$sendet_interface['mtu'],
(int)$sendet_interface['traffic_rx'], (int)$traffic_rx_per_second_byte,
(int)$sendet_interface['traffic_tx'], (int)$traffic_tx_per_second_byte,
$sendet_interface['wlan_mode'], $sendet_interface['wlan_frequency'], $sendet_interface['wlan_essid'], $sendet_interface['wlan_bssid'],
(int)$sendet_interface['wlan_tx_power'], false);
$networkinterface_status->store();
//TODO: Remove networkinterfaces that are not linked to any status, service, habe no ip addresses and are not marked as protected
//Update RRD Graph DB
$rrd_path_traffic_rx = __DIR__."/rrdtool/databases/router_$_POST[router_id]_interface_$sendet_interface[name]_traffic_rx.rrd";
if(!file_exists($rrd_path_traffic_rx)) {
//Create new RRD-Database
exec("rrdtool create $rrd_path_traffic_rx --step 600 --start ".time()." DS:traffic_rx:GAUGE:700:U:U DS:traffic_tx:GAUGE:900:U:U RRA:AVERAGE:0:1:144 RRA:AVERAGE:0:6:168 RRA:AVERAGE:0:18:240");
}
$interface_last_endet_crawl = Interfaces::getInterfaceCrawlByCrawlCycleAndRouterIdAndInterfaceName($last_endet_crawl_cycle['id'], $_POST['router_id'], $sendet_interface['name']);
$interface_crawl_data['traffic_info']['traffic_rx_per_second_byte'] = ($sendet_interface['traffic_rx']-$interface_last_endet_crawl['traffic_rx'])/$GLOBALS['crawl_cycle']/60;
//Set negative values to 0
if ($interface_crawl_data['traffic_info']['traffic_rx_per_second_byte']<0)
$interface_crawl_data['traffic_info']['traffic_rx_per_second_byte']=0;
$interface_crawl_data['traffic_info']['traffic_rx_per_second_kibibyte'] = round($interface_crawl_data['traffic_info']['traffic_rx_per_second_byte']/1024, 2);
$interface_crawl_data['traffic_info']['traffic_rx_per_second_kilobyte'] = round($interface_crawl_data['traffic_info']['traffic_rx_per_second_byte']/1000, 2);
$interface_crawl_data['traffic_info']['traffic_tx_per_second_byte'] = ($sendet_interface['traffic_tx']-$interface_last_endet_crawl['traffic_tx'])/$GLOBALS['crawl_cycle']/60;
//Set negative values to 0
if ($interface_crawl_data['traffic_info']['traffic_tx_per_second_byte']<0)
$interface_crawl_data['traffic_info']['traffic_tx_per_second_byte']=0;
$interface_crawl_data['traffic_info']['traffic_tx_per_second_kibibyte'] = round($interface_crawl_data['traffic_info']['traffic_tx_per_second_byte']/1024, 2);
$interface_crawl_data['traffic_info']['traffic_tx_per_second_kilobyte'] = round($interface_crawl_data['traffic_info']['traffic_tx_per_second_byte']/1000, 2);
//Update Database
$crawl_time = time();
exec("rrdtool update $rrd_path_traffic_rx $crawl_time:".$interface_crawl_data['traffic_info']['traffic_rx_per_second_kilobyte'].":".$interface_crawl_data['traffic_info']['traffic_tx_per_second_kilobyte']);
}
/**Insert Batman advanced Interfaces*/
foreach($_POST['bat_adv_int'] as $bat_adv_int) {
try {
$stmt = DB::getInstance()->prepare("INSERT INTO crawl_batman_advanced_interfaces (router_id, crawl_cycle_id, name, status, crawl_date)
VALUES (:router_id, :crawl_cycle_id, :bat_adv_int_name, :bat_adv_int_status, NOW())");
$stmt->execute(array(
':router_id' => $_POST['router_id'],
':crawl_cycle_id' => $actual_crawl_cycle['id'],
':bat_adv_int_name' => $bat_adv_int['name'],
':bat_adv_int_status' => $bat_adv_int['status']
));
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
/**Insert Batman Advanced Originators*/
if(!empty($_POST['bat_adv_orig'])) {
foreach($_POST['bat_adv_orig'] as $bat_adv_orig) {
try {
$stmt = DB::getInstance()->prepare("INSERT INTO crawl_batman_advanced_originators (router_id, crawl_cycle_id, originator, link_quality, nexthop, outgoing_interface, last_seen, crawl_date)
VALUES (:router_id, :crawl_cycle_id, :originator, :link_quality, :nexthop, :outgoing_interface, :last_seen, NOW());");
$stmt->execute(array(
':router_id' => $_POST['router_id'],
':crawl_cycle_id' => $actual_crawl_cycle['id'],
':originator' => $bat_adv_orig['originator'],
':link_quality' => $bat_adv_orig['link_quality'],
':nexthop' => $bat_adv_orig['nexthop'],
':outgoing_interface' => $bat_adv_orig['outgoing_interface'],
':last_seen' => $bat_adv_orig[last_seen]
));
}
catch(PDOException $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
}
RrdTool::updateRouterBatmanAdvOriginatorLinkQuality($_POST['router_id'], $bat_adv_orig['originator'], $bat_adv_orig['link_quality'], time());
}
}
$originator_count=count($_POST['bat_adv_orig']);
RrdTool::updateRouterBatmanAdvOriginatorsCountHistory($_POST['router_id'], $originator_count);
$average_link_quality = 0;
foreach($_POST['bat_adv_orig'] as $originator) {
$average_link_quality=$average_link_quality+$originator['link_quality'];
}
$average_link_quality=($average_link_quality/$originator_count);
RrdTool::updateRouterBatmanAdvOriginatorLinkQuality($_POST['router_id'], "average", $average_link_quality, time());
RrdTool::updateRouterClientCountHistory($_POST['router_id'], $_POST['client_count']);
} else {
echo "Your router with the id $_POST[router_id] has already been crawled";
}
} else {
echo "error;";
echo "You FAILED! to authenticated at netmon api nodewatcher section insert_crawl_interfaces_data\n";
echo "Your router_id is: ".$_POST['router_id'];
echo "Your authentificationmethod is: ".$_POST['authentificationmethod'];
echo "Your netmon router_auto_assign_hash is: ".$router_data['router_auto_assign_hash'];
echo "Your router_auto_update_hash is: ".$_POST['router_auto_update_hash'];
}
}
?>