-
Notifications
You must be signed in to change notification settings - Fork 1
/
cronjobs.php
174 lines (152 loc) · 6.34 KB
/
cronjobs.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
<?php
//This script can only be called by the server
if(!empty($_SERVER['REMOTE_ADDR'])) {
die("This script can only be run by the server directly.");
}
/**
* Necessary includes
**/
require_once('runtime.php');
require_once(ROOT_DIR.'/lib/core/crawling.class.php');
require_once(ROOT_DIR.'/lib/core/router_old.class.php');
require_once(ROOT_DIR.'/lib/core/rrdtool.class.php');
require_once(ROOT_DIR.'/lib/core/Eventlist.class.php');
require_once(ROOT_DIR.'/lib/core/RouterStatus.class.php');
require_once(ROOT_DIR.'/lib/core/Networkinterfacelist.class.php');
require_once(ROOT_DIR.'/lib/core/NetworkinterfaceStatus.class.php');
require_once(ROOT_DIR.'/lib/core/EventNotificationList.class.php');
/**
* Crawlcycle organisation
**/
echo "Organizing crawl cycles\n";
//Get crawl cycle data
$actual_crawl_cycle = Crawling::getActualCrawlCycle();
$crawl_cycle_start_buffer_in_minutes = ConfigLine::configByName("crawl_cycle_start_buffer_in_minutes");
if(empty($actual_crawl_cycle) OR (strtotime($actual_crawl_cycle['crawl_date'])+(($GLOBALS['crawl_cycle']-$crawl_cycle_start_buffer_in_minutes)*60))<=time()) {
echo "Create crawl data for offline routers\n";
//Set all routers in old crawl cycle that have not been crawled yet to status offline
try {
$stmt = DB::getInstance()->prepare("SELECT *
FROM routers
WHERE id not in (SELECT router_id
FROM crawl_routers
WHERE crawl_cycle_id=$actual_crawl_cycle[id])");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
}
foreach($result as $router) {
echo " Inserting offline data for router ".$router['hostname']."\n";
//store offline crawl for offline router
$router_status = New RouterStatus(false, (int)$actual_crawl_cycle['id'], (int)$router['id'], "offline");
$router_status->store();
}
echo "Create crawl data for offline interfaces\n";
//store offline crawl for all interfaces of offline router
try {
$stmt = DB::getInstance()->prepare("SELECT *
FROM interfaces
WHERE id not in (SELECT interface_id
FROM crawl_interfaces
WHERE crawl_cycle_id=$actual_crawl_cycle[id])");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
}
//Use custom multiinsert statement instead of InterfaceStatus class
echo " Prepare insertion of offline data for interfaces\n";
$placeholders = array();
$values = array();
foreach($result as $interface) {
$placeholders[] = "(?, ?, ?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
array_push($values, (int)$interface['router_id'], (int)$actual_crawl_cycle['id'], (int)$interface['id'], "", "", 0, 0, 0, 0, "", "", "", "", 0, 0);
}
echo " Insert offline Data for interfaces in one big statement\n";
$stmt = DB::getInstance()->prepare("INSERT INTO crawl_interfaces (router_id, crawl_cycle_id, interface_id, crawl_date,
name, mac_addr, traffic_rx, traffic_rx_avg,
traffic_tx, traffic_tx_avg,
wlan_mode, wlan_frequency, wlan_essid, wlan_bssid,
wlan_tx_power, mtu)
VALUES ".implode(", ", $placeholders));
try {
$stmt->execute($values);
} catch(PDOException $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
}
echo "Close old crawl cycle and create new one\n";
//Create new crawl cycle and close old crawl cycle
//Create new crawl cycle
Crawling::newCrawlCycle();
//Close old Crawl cycle
Crawling::closeCrawlCycle($actual_crawl_cycle['id']);
echo "Create graph statistics\n";
//Make statistic graphs
$online = Router_old::countRoutersByCrawlCycleIdAndStatus($actual_crawl_cycle['id'], 'online');
$offline = Router_old::countRoutersByCrawlCycleIdAndStatus($actual_crawl_cycle['id'], 'offline');
$unknown = Router_old::countRoutersByCrawlCycleIdAndStatus($actual_crawl_cycle['id'], 'unknown');
$total = $unknown+$offline+$online;
RrdTool::updateNetmonHistoryRouterStatus($online, $offline, $unknown, $total);
$client_count = Router_old::countRoutersByCrawlCycleId($actual_crawl_cycle['id']);
RrdTool::updateNetmonClientCount($client_count);
/**
* Clean database
*/
echo "Clean database\n";
//Delete old Crawls
echo "Remove old crawl data\n";
Crawling::deleteOldCrawlDataExceptLastOnlineCrawl(($GLOBALS['hours_to_keep_mysql_crawl_data']*60*60));
//Remove old events
echo "Remove old events\n";
$secondsToKeepHistoryTable = 60*60*$GLOBALS['hours_to_keep_history_table'];
try {
$stmt = DB::getInstance()->prepare("DELETE FROM events WHERE UNIX_TIMESTAMP(create_date) < UNIX_TIMESTAMP(NOW())-?");
$stmt->execute(array($secondsToKeepHistoryTable));
} catch(PDOException $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
}
//Remove old not assigned routers
echo "Remove not assigned routers that haven´t been updated for a while\n";
DB::getInstance()->exec("DELETE FROM routers_not_assigned WHERE TO_DAYS(update_date) < TO_DAYS(NOW())-2");
/**
* Crawl
**/
echo "Do crawling\n";
//Crawl routers
echo "Crawl routers\n";
if (!isset($GLOBALS['crawllog']))
$GLOBALS['crawllog'] = false;
if ($GLOBALS['crawllog'] && !is_dir(ROOT_DIR."/logs/")) {
mkdir(ROOT_DIR."/logs/");
}
$range = ConfigLine::configByName("crawl_range");
$routers_count = Router_old::countRouters();
for ($i=0; $i<=$routers_count; $i+=$range) {
//start an independet crawl process for each $range routers to crawl routers simultaniously
$return = array();
if ($GLOBALS['crawllog'])
$logcmd = "> ".ROOT_DIR."/logs/crawler_".$i."-".($i+$range).".txt";
else
$logcmd = "> /dev/null";
$cmd = "php ".ROOT_DIR."/integrated_xml_ipv6_crawler.php -o".$i." -l".$range." ".$logcmd." & echo $!";
echo "Initializing crawl process to crawl routers ".$i." to ".($i+$range)."\n";
echo "Running command: $cmd\n";
exec($cmd, $return);
echo "The initialized crawl process has the pid $return[0]\n";
}
/**
* Notifications
*/
echo "Sending notifications\n";
$event_notification_list = new EventNotificationList();
$event_notification_list->notify();
} else {
echo "There is an crawl cycle running actually. Doing nothing.\n";
}
echo "Done\n";
?>