-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcrashplan_model.php
97 lines (89 loc) · 3.22 KB
/
crashplan_model.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
<?php
class Crashplan_model extends \Model
{
public function __construct($serial = '')
{
parent::__construct('id', 'crashplan'); //primary key, tablename
$this->rs['id'] = '';
$this->rs['serial_number'] = $serial; //$this->rt['serial_number'] = 'VARCHAR(255) UNIQUE';
$this->rs['destination'] = ''; // Name of destination
$this->rs['last_success'] = 0; // Timestamp of last successful backup
$this->rs['duration'] = 0; // duration in seconds
$this->rs['last_failure'] = 0; // Timestamp of last failed backup
$this->rs['reason'] = ''; // Reason of last failure
}
// ------------------------------------------------------------------------
/**
* Process data sent by postflight
*
* @param string data
*
**/
public function process($data)
{
// Delete previous entries
$serial_number = $this->serial_number;
$this->deleteWhere('serial_number=?', $serial_number);
//
$messages = array(
'errors' => array(),
'warnings' => array()
);
// Parse data
$lines = explode("\n", $data);
$headers = str_getcsv(array_shift($lines));
foreach ($lines as $line) {
if ($line) {
$this->merge(array_combine($headers, str_getcsv($line)));
// Only store entry when there is at least one date
if ($this->last_success > 0 or $this->last_failure > 0) {
$this->id = '';
$this->serial_number = $serial_number;
$this->save();
// Events
if ($this->last_success < $this->last_failure) {
$messages['errors'][] = array(
'destination' => $this->destination,
'reason' => $this->reason,
);
}
}
}
}
// Only store if there is data
if ($messages['errors']) {
$type = 'danger';
$msg = 'crashplan.backup_failed';
if (count($messages['errors']) == 1) {
$out = array_pop($messages['errors']);
} else {
$out = array('count' => count($messages['errors']));
}
$data = json_encode($out);
$this->store_event($type, $msg, $data);
} else {
$this->delete_event();
}
} // end process()
/**
* Get statistics
*
* @return void
* @author
**/
public function get_stats($hours)
{
$now = time();
$today = $now - 3600 * 24;
$week_ago = $now - 3600 * 24 * 7;
$month_ago = $now - 3600 * 24 * 30;
$sql = "SELECT COUNT(1) as total,
COUNT(CASE WHEN last_success > '$today' THEN 1 END) AS today,
COUNT(CASE WHEN last_success BETWEEN '$week_ago' AND '$today' THEN 1 END) AS lastweek,
COUNT(CASE WHEN last_success < '$week_ago' THEN 1 END) AS week_plus
FROM crashplan
LEFT JOIN reportdata USING (serial_number)
".get_machine_group_filter();
return current($this->query($sql));
}
}