-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmanagedinstalls_controller.php
243 lines (212 loc) · 7.17 KB
/
managedinstalls_controller.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
<?php
/**
* managedinstalls class
*
* @package munkireport
* @author
**/
class managedinstalls_controller extends Module_controller
{
protected $module_path;
protected $view_path;
/*** Protect methods with auth! ****/
public function __construct()
{
// Store module path
$this->module_path = dirname(__FILE__);
$this->view_path = dirname(__FILE__) . '/views/';
}
/**
* Get managedinstalls information for serial_number
*
* @param string $serial serial number
**/
public function get_data($serial_number = '')
{
$out = array();
if (! $this->authorized()) {
$out['error'] = 'Not authorized';
} else {
$out = Managedinstalls_model::where('managedinstalls.serial_number', $serial_number)
->filter()
->get();
}
$obj = new View();
$obj->view('json', array('msg' => $out));
}
// ------------------------------------------------------------------------
/**
* Get pending installs
*
*
* @param string $type Type, munki or apple
**/
public function get_pending_installs($type = "munki")
{
jsonView($this->get_pending_items('pending_install', $type));
}
// ------------------------------------------------------------------------
/**
* Get pending removals
*
*
* @param string $type Type, munki or apple
**/
public function get_pending_removals($type = "munki")
{
jsonView($this->get_pending_items('pending_removal', $type));
}
// ------------------------------------------------------------------------
/**
* Get pending items
*
*
* @param string $status Type, pending_removal or pending_install
* @param string $type Type, munki or apple
**/
private function get_pending_items($status, $type)
{
$hoursBack = 24 * 7; // Hours back
$fromdate = time() - 3600 * $hoursBack;
$query = Managedinstalls_model::selectRaw('name, version, display_name, COUNT(*) as count')
->where('managedinstalls.status', $status)
->where('reportdata.timestamp', '>', $fromdate)
->where('type', $type)
->filter()
->groupBy('name', 'display_name', 'version')
->orderBy('count', 'desc');
return $query->get()->toArray();
}
// ------------------------------------------------------------------------
/**
* Get package statistics
*
* Get statistics about a packat
*
* @param string name Package name
* @return {11:return type}
*/
public function get_pkg_stats($pkg = '')
{
$out = array();
if (! $this->authorized()) {
$out['error'] = 'Not authorized';
} else {
$query = Managedinstalls_model::selectRaw('name, version, display_name, managedinstalls.status, COUNT(*) as count')
->filter()
->groupBy('managedinstalls.status', 'name', 'display_name', 'version')
->orderBy('version', 'desc');
if ($pkg) {
$query = $query->where('name', '=', $pkg);
}
// Convert to list
foreach ($query->get()->toArray() as $rs) {
$status = $rs['status'] == 'install_succeeded' ? 'installed' : $rs['status'];
$key = $rs['name'] . $rs['version'];
if (isset($out[$key])) {
if (isset($out[$key][$status])) {
// $key exists, add count
$out[$key][$status] += $rs['count'];
} else {
$out[$key][$status] = $rs['count'];
}
} else {
$out[$key] = array(
'name' => $rs['name'],
'version' => $rs['version'],
'display_name' => $rs['display_name'],
$status => $rs['count'],
);
}
}
}
$obj = new View();
$obj->view('json', array('msg' => array_values($out)));
}
/**
* Get installs statistics
*
* Undocumented function long description
*
* @param int $hours number of hours back or 0 for all
* @return {11:return type}
*/
public function get_stats($hours = 0)
{
$out = array();
if (! $this->authorized()) {
$out['error'] = 'Not authorized';
} else {
if ($hours > 0) {
$timestamp = time() - 60 * 60 * $hours;
} else {
$timestamp = 0;
}
$query = Managedinstalls_model::selectRaw('managedinstalls.status, type, count(distinct reportdata.serial_number) as clients, count(managedinstalls.status) as total_items')
->join('machine', 'machine.serial_number', '=', 'managedinstalls.serial_number')
->where('reportdata.timestamp', '>', $timestamp)
->whereNotNull('managedinstalls.type')
->filter()
->groupBy('managedinstalls.status', 'type');
$out = $query->get()->toArray();
}
$obj = new View();
$obj->view('json', array('msg' => $out));
}
/**
* undocumented function summary
*
* Undocumented function long description
*
* @param type var Description
* @return {11:return type}
*/
public function listing($name = '', $version = '')
{
if (! $this->authorized()) {
redirect('auth/login');
}
$data['name'] = addslashes($name);
$data['version'] = addslashes($version);
$data['page'] = 'clients';
$data['scripts'] = array("clients/client_list.js");
$obj = new View();
$obj->view('managed_installs_listing', $data, $this->view_path);
}
/**
* View a file
* TODO: move to parent?
* @param string $page filename
*/
public function view($page)
{
if (! $this->authorized()) {
redirect('auth/login');
}
$obj = new View();
$obj->view($page, '', $this->view_path);
}
/**
* Get machines with $status installs
* *
* @param integer $hours Number of hours to get stats from
**/
public function get_clients($status = 'pending_install', $hours = 24)
{
$out = [];
if (! $this->authorized()) {
$out['error'] = 'Not authorized';
} else {
$timestamp = time() - 60 * 60 * $hours;
$query = Managedinstalls_model::selectRaw('computer_name, machine.serial_number, COUNT(*) as count')
->join('machine', 'machine.serial_number', '=', 'managedinstalls.serial_number')
-> where('managedinstalls.status', $status)
->filter()
->groupBy('machine.serial_number', 'computer_name')
->orderBy('count', 'desc');
$out = $query->get()->toArray();
}
$obj = new View();
$obj->view('json', array('msg' => $out));
}
} // END class default_module