Repository navigation
Expand file tree
/
Copy pathcaching_controller.php
More file actions
executable file
·492 lines (429 loc) · 20.2 KB
/
Copy pathcaching_controller.php
File metadata and controls
executable file
·492 lines (429 loc) · 20.2 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
/**
* Caching_controller class
*
* @package munkireport
* @author @tuxudo
**/
class Caching_controller extends Module_controller
{
public function __construct()
{
// No authentication, the client needs to get here
// Store module path
$this->module_path = dirname(__FILE__);
}
/**
* Default method
*
* @author AvB
**/
public function index()
{
echo "You've loaded the caching report module!";
}
/**
* Get data for scroll widget
*
* @return void
* @author tuxudo
**/
public function get_reachable_cache_name()
{
$sql = "SELECT COUNT(CASE WHEN reachability <> '' AND reachability IS NOT NULL THEN 1 END) AS count, reachability
FROM caching
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter()."
GROUP BY reachability
ORDER BY count DESC";
$queryobj = new Caching_model;
$out = [];
foreach ($queryobj->query($sql) as $obj) {
if ("$obj->count" !== "0") {
$obj->reachability = $obj->reachability ? $obj->reachability : 'Unknown';
$out[] = $obj;
}
}
jsonView($out);
}
/**
* Retrieve data in json format for client tab
*
* @return void
* @author tuxudo
**/
public function get_client_tab_data($serial_number = '')
{
// Remove non-serial number characters
$serial_number = preg_replace("/[^A-Za-z0-9_\-]/", '', $serial_number);
$obj = new View();
if (! $this->authorized()) {
$obj->view('json', array('msg' => 'Not authorized'));
return;
}
$queryobj = new Caching_model();
// Check which version of data to return
$sqlcheck = "SELECT startupstatus, expirationdate
FROM caching
WHERE serial_number = '$serial_number'";
$caching_check = $queryobj->query($sqlcheck);
if ( ! empty($caching_check[0]->startupstatus) && ! $caching_check[0]->expirationdate == -1) {
// If column startupstatus is not empty/null, run SQL query for new dataset
$sql = "SELECT ' ' AS 'groupdate',
activated AS 'activated',
active AS 'active',
cachestatus AS 'cachestatus',
startupstatus AS 'startupstatus',
reachability AS 'reachability',
cachefree AS 'cachefree',
cacheused AS 'cacheused',
cachelimit AS 'cachelimit',
macsoftware AS 'macsoftware',
appletvsoftware AS 'appletvsoftware',
iossoftware AS 'iossoftware',
iclouddata AS 'iclouddata',
booksdata AS 'booksdata',
itunesudata AS 'itunesudata',
moviesdata AS 'moviesdata',
musicdata AS 'musicdata',
otherdata AS 'otherdata',
personalcachefree AS 'personalcachefree',
personalcacheused AS 'personalcacheused',
personalcachelimit AS 'personalcachelimit',
publicaddress AS 'publicaddress',
privateaddresses AS 'privateaddresses',
port AS 'port',
registrationstatus AS 'registrationstatus',
registrationerror AS 'registrationerror',
registrationresponsecode AS 'registrationresponsecode',
restrictedmedia AS 'restrictedmedia',
serverguid AS 'serverguid',
totalbytesreturnedtoclients AS 'totalbytesreturnedtoclients',
totalbytesreturnedtochildren AS 'totalbytesreturnedtochildren',
totalbytesreturnedtopeers AS 'totalbytesreturnedtopeers',
totalbytesstoredfromorigin AS 'totalbytesstoredfromorigin',
totalbytesstoredfromparents AS 'totalbytesstoredfromparents',
totalbytesstoredfrompeers AS 'totalbytesstoredfrompeers',
totalbytesdropped AS 'totalbytesdropped',
totalbytesimported AS 'totalbytesimported'
FROM caching
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter()."
AND serial_number = '$serial_number'";
$caching_tab = $queryobj->query($sql);
} else if ( ! empty($caching_check[0]->startupstatus) && $caching_check[0]->expirationdate == -1) {
// If column startupstatus is not empty/null and expirationdate is flagged, run SQL queries for new dataset
$sql = "SELECT '' AS 'groupdate',
activated AS 'activated',
active AS 'active',
cachestatus AS 'cachestatus',
startupstatus AS 'startupstatus',
reachability AS 'reachability',
cachefree AS 'cachefree',
cacheused AS 'cacheused',
cachelimit AS 'cachelimit',
macsoftware AS 'macsoftware',
appletvsoftware AS 'appletvsoftware',
iossoftware AS 'iossoftware',
iclouddata AS 'iclouddata',
booksdata AS 'booksdata',
itunesudata AS 'itunesudata',
moviesdata AS 'moviesdata',
musicdata AS 'musicdata',
otherdata AS 'otherdata',
personalcachefree AS 'personalcachefree',
personalcacheused AS 'personalcacheused',
personalcachelimit AS 'personalcachelimit',
publicaddress AS 'publicaddress',
privateaddresses AS 'privateaddresses',
port AS 'port',
registrationstatus AS 'registrationstatus',
registrationerror AS 'registrationerror',
registrationresponsecode AS 'registrationresponsecode',
restrictedmedia AS 'restrictedmedia',
serverguid AS 'serverguid',
totalbytesreturnedtoclients AS 'totalbytesreturnedtoclients',
totalbytesreturnedtochildren AS 'totalbytesreturnedtochildren',
totalbytesreturnedtopeers AS 'totalbytesreturnedtopeers',
totalbytesstoredfromorigin AS 'totalbytesstoredfromorigin',
totalbytesstoredfromparents AS 'totalbytesstoredfromparents',
totalbytesstoredfrompeers AS 'totalbytesstoredfrompeers',
totalbytesdropped AS 'totalbytesdropped',
totalbytesimported AS 'totalbytesimported'
FROM caching
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter()."
AND serial_number = '$serial_number' AND activated <> ''";
// Get the new 10.13+ data
$caching_tab_new = $queryobj->query($sql);
$sql = "SELECT substr(collectiondate,1,10) AS 'groupdate',
SUM(bytesfromcachetoclients) AS 'bytesfromcachetoclients',
SUM(bytesfromorigintoclients) AS 'bytesfromorigintoclients',
SUM(bytesfrompeerstoclients) AS 'bytesfrompeerstoclients',
SUM(bytesfromcachetopeers) AS 'bytesfromcachetopeers',
SUM(bytesfromorigintopeers) AS 'bytesfromorigintopeers',
SUM(requestsfromclients) AS 'requestsfromclients',
SUM(requestsfrompeers) AS 'requestsfrompeers',
SUM(bytespurgedyoungerthan1day) AS 'bytespurgedyoungerthan1day',
SUM(bytespurgedyoungerthan7days) AS 'bytespurgedyoungerthan7days',
SUM(bytespurgedyoungerthan30days) AS 'bytespurgedyoungerthan30days',
SUM(bytespurgedtotal) AS 'bytespurgedtotal',
SUM(bytesdropped) AS 'bytesdropped',
SUM(repliesfromcachetoclients) AS 'repliesfromcachetoclients',
SUM(repliesfromorigintoclients) AS 'repliesfromorigintoclients',
SUM(repliesfrompeerstoclients) AS 'repliesfrompeerstoclients',
SUM(repliesfromcachetopeers) AS 'repliesfromcachetopeers',
SUM(repliesfromorigintopeers) AS 'repliesfromorigintopeers',
SUM(bytesimportedbyhttp) AS 'bytesimportedbyhttp',
SUM(bytesimportedbyxpc) AS 'bytesimportedbyxpc',
SUM(importsbyhttp) AS 'importsbyhttp',
SUM(importsbyxpc) AS 'importsbyxpc'
FROM caching
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter()."
AND serial_number = '$serial_number' AND collectiondate <> ''
GROUP BY groupdate
ORDER BY groupdate ASC";
// Get the legacy/10.13.4+ data
$caching_tab_legacy = $queryobj->query($sql);
if (array_key_exists(0, $caching_tab_new)){
// Push the new 10.13+ data into the legacy/10.13.4+ array if we have 10.13+ data
array_push($caching_tab_legacy,$caching_tab_new[0]);
}
// Reverse the array so that the 10.13+ data is at the top
$caching_tab = array_reverse($caching_tab_legacy);
} else {
// Else run legacy SQL query
$sql = "SELECT substr(collectiondate,1,10) AS 'groupdate',
SUM(bytesfromcachetoclients) AS 'bytesfromcachetoclients',
SUM(bytesfromorigintoclients) AS 'bytesfromorigintoclients',
SUM(bytesfrompeerstoclients) AS 'bytesfrompeerstoclients',
SUM(bytesfromcachetopeers) AS 'bytesfromcachetopeers',
SUM(bytesfromorigintopeers) AS 'bytesfromorigintopeers',
SUM(requestsfromclients) AS 'requestsfromclients',
SUM(requestsfrompeers) AS 'requestsfrompeers',
SUM(bytespurgedyoungerthan1day) AS 'bytespurgedyoungerthan1day',
SUM(bytespurgedyoungerthan7days) AS 'bytespurgedyoungerthan7days',
SUM(bytespurgedyoungerthan30days) AS 'bytespurgedyoungerthan30days',
SUM(bytespurgedtotal) AS 'bytespurgedtotal',
SUM(bytesdropped) AS 'bytesdropped',
SUM(repliesfromcachetoclients) AS 'repliesfromcachetoclients',
SUM(repliesfromorigintoclients) AS 'repliesfromorigintoclients',
SUM(repliesfrompeerstoclients) AS 'repliesfrompeerstoclients',
SUM(repliesfromcachetopeers) AS 'repliesfromcachetopeers',
SUM(repliesfromorigintopeers) AS 'repliesfromorigintopeers',
SUM(bytesimportedbyhttp) AS 'bytesimportedbyhttp',
SUM(bytesimportedbyxpc) AS 'bytesimportedbyxpc',
SUM(importsbyhttp) AS 'importsbyhttp',
SUM(importsbyxpc) AS 'importsbyxpc'
FROM caching
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter()."
AND serial_number = '$serial_number'
GROUP BY groupdate
ORDER BY groupdate DESC";
$caching_tab = $queryobj->query($sql);
}
$obj->view('json', array('msg' => current(array('msg' => $caching_tab))));
}
/**
* REST API for retrieving caching data for chart
* @tuxudo
*
**/
public function caching_graph()
{
$obj = new View();
if (! $this->authorized()) {
$obj->view('json', array('msg' => 'Not authorized'));
return;
}
$cachingdata = new Caching_model();
$pastThirtyOne = (time()-2678400);
switch ($cachingdata->get_driver()) {
case 'sqlite':
$sql = "SELECT DATE(collectiondateepoch, 'unixepoch') AS date,
sum(bytesfromcachetoclients) AS fromcache,
sum(bytesfromorigintoclients) AS fromorigin,
sum(bytespurgedtotal) AS purgedbytes
FROM caching
LEFT JOIN reportdata USING (serial_number)
WHERE collectiondateepoch > ".$pastThirtyOne."
".get_machine_group_filter('AND')."
GROUP BY date
ORDER BY date";
break;
case 'mysql':
$sql = "SELECT DATE(FROM_UNIXTIME(collectiondateepoch)) AS date,
sum(bytesfromcachetoclients) AS fromcache,
sum(bytesfromorigintoclients) AS fromorigin,
sum(bytespurgedtotal) AS purgedbytes
FROM caching
LEFT JOIN reportdata USING (serial_number)
WHERE collectiondateepoch > ".$pastThirtyOne."
".get_machine_group_filter('AND')."
GROUP BY date
ORDER BY date";
break;
default:
die('Unknown database driver');
}
$dates = array();
$cache = array();
$origin = array();
$purged = array();
foreach ($cachingdata->query($sql) as $event) {
$dates[] = $event->date;
$cache[] = $event->fromcache;
$origin[] = $event->fromorigin;
$purged[] = $event->purgedbytes;
}
// Add in padded date to fix graph chop off
$datetime = new DateTime('tomorrow');
$dates[] = $datetime->format('Y-m-d');
$cache[] = 0;
$origin[] = 0;
$purged[] = 0;
$numbervar = range(1,count($dates));
$cacheassoc = array();
foreach ($numbervar as $i => $value) {
$cacheassoc[$value] = intval($cache[$i]);
}
$originassoc = array();
foreach ($numbervar as $i => $value) {
$originassoc[$value] = intval($origin[$i]);
}
$purgedassoc = array();
foreach ($numbervar as $i => $value) {
$purgedassoc[$value] = intval($purged[$i]);
}
$obj->view('json', array('msg' => array('dates' => $dates, 'cache' => $cacheassoc, 'origin' => $originassoc, 'purged' => $purgedassoc)));
}
/**
* REST API for retrieving caching data for widget
* @tuxudo
*
**/
public function caching_widget()
{
$obj = new View();
if (! $this->authorized()) {
$obj->view('json', array('msg' => 'Not authorized'));
return;
}
$queryobj = new Caching_model();
$sql = "SELECT sum(bytesfromcachetoclients) AS fromcache,
sum(bytespurgedtotal) AS purgedbytes,
sum(bytesfromorigintoclients) AS fromorigin
FROM caching
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter();
$caching_array = $queryobj->query($sql);
$obj->view('json', array('msg' => current(array('msg' => $caching_array[0]))));
}
/**
* REST API for retrieving media caching data for media widget
* @tuxudo
*
**/
public function caching_media_widget()
{
$obj = new View();
if (! $this->authorized()) {
$obj->view('json', array('msg' => 'Not authorized'));
return;
}
$queryobj = new Caching_model();
$sql = "SELECT sum(booksdata) AS books,
sum(musicdata) AS music,
sum(moviesdata) AS movies
FROM caching
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter();
$caching_array = $queryobj->query($sql);
$obj->view('json', array('msg' => current(array('msg' => $caching_array[0]))));
}
/**
* REST API for retrieving software caching data for software widget
* @tuxudo
*
**/
public function caching_software_widget()
{
$obj = new View();
if (! $this->authorized()) {
$obj->view('json', array('msg' => 'Not authorized'));
return;
}
$queryobj = new Caching_model();
$sql = "SELECT sum(appletvsoftware) AS appletv,
sum(macsoftware) AS mac,
sum(iossoftware) AS ios
FROM caching
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter();
$caching_array = $queryobj->query($sql);
$obj->view('json', array('msg' => current(array('msg' => $caching_array[0]))));
}
/**
* REST API for retrieving icloud caching data for iCloud widget
* @tuxudo
*
**/
public function caching_icloud_widget()
{
$obj = new View();
if (! $this->authorized()) {
$obj->view('json', array('msg' => 'Not authorized'));
return;
}
$queryobj = new Caching_model();
$sql = "SELECT sum(iclouddata) AS icloud,
sum(itunesudata) AS itunesu,
sum(otherdata) AS other
FROM caching
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter();
$caching_array = $queryobj->query($sql);
$obj->view('json', array('msg' => current(array('msg' => $caching_array[0]))));
}
/**
* REST API for retrieving caching data for usage widget
* @tuxudo
*
**/
public function caching_usage_widget()
{
$obj = new View();
if (! $this->authorized()) {
$obj->view('json', array('msg' => 'Not authorized'));
return;
}
$queryobj = new Caching_model();
$sql = "SELECT SUM(cachefree) AS cachefree,
SUM(cachelimit) AS cachelimit,
SUM(cacheused) AS cacheused
FROM caching
LEFT JOIN reportdata USING (serial_number)
WHERE activated = 1
".get_machine_group_filter('AND');
$caching_array = $queryobj->query($sql);
$obj->view('json', array('msg' => current(array('msg' => $caching_array[0]))));
}
/**
* Retrieve data in json format
*
* @return void
* @author tuxudo
**/
public function get_data($serial_number = '')
{
$obj = new View();
if (! $this->authorized()) {
$obj->view('json', array('msg' => 'Not authorized'));
return;
}
$caching = new Caching_model;
$obj->view('json', array('msg' => $caching->retrieve_records($serial_number)));
}
} // END class Caching_controller