-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkandji_controller.php
executable file
·182 lines (159 loc) · 5.55 KB
/
kandji_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
<?php
/**
* Kandji module class
*
* @package munkireport
* @author jc0b
**/
use munkireport\models\MRModel as Eloquent;
use Illuminate\Support\Facades\DB;
class kandji_controller extends Module_controller
{
public function __construct()
{
// No authentication, the client needs to get here
// Store module path
$this->module_path = dirname(__FILE__);
// Add local config
configAppendFile(__DIR__ . '/config.php');
}
public function index()
{
echo "You've loaded the Kandji module!";
}
// Add the admin page
public function admin()
{
$obj = new View();
$obj->view('kandji_admin', [], $this->module_path.'/views/');
}
/**
* Get Kandji version for widget
*
* @return void
* @author jc0b
**/
public function get_kandji_version()
{
$kandji_version_data = Kandji_model::selectRaw("COALESCE(SUM(CASE WHEN kandji_agent_version IS NOT NULL THEN 1 END), 0) AS count, kandji_agent_version")->filter()->groupBy('kandji_agent_version')->orderBy('count', 'desc')->get()->toArray();
$obj = new View();
$obj->view('json', array('msg' => $kandji_version_data));
}
/**
* REST API for retrieving last checkin data for widget
*
* @author jc0b
**/
public function get_last_checkin()
{
$currentdate = date_timestamp_get(date_create());
$week = $currentdate - 604800;
$month = $currentdate - 2592000;
$checkin_data = Kandji_model::selectRaw("COALESCE(SUM(CASE WHEN last_check_in <= $month THEN 1 END), 0) AS red,
COALESCE(SUM(CASE WHEN last_check_in <= $week AND last_check_in > $month THEN 1 END), 0) AS yellow,
COALESCE(SUM(CASE WHEN last_check_in > $week AND last_check_in > 0 THEN 1 END), 0) AS green")
->filter()
->first()
->toLabelCount();
$obj = new View();
$obj->view('json', array('msg' => $checkin_data));
}
/**
* REST API for retrieving stats on passport enablement
*
* @author jc0b
**/
public function get_passport_stats()
{
$passport_data = Kandji_model::selectRaw("COALESCE(SUM(case when passport_enabled = 'True' THEN 1 END), 0) AS enabled,
COALESCE(SUM(case when passport_enabled <> 'True' THEN 1 END), 0) AS disabled")
->filter()
->first()
->toLabelCount();
$obj = new View();
$obj->view('json', array('msg' => $passport_data));
}
/**
* Pull in Kandji data for all serial numbers :D
*
* @return void
* @author tuxudo
**/
public function pull_all_kandji_data($incoming_serial = '')
{
// Check if we are returning a list of all serials or processing a serial
// Returns either a list of all serial numbers in MunkiReport OR
// a JSON of what serial number was just ran with the status of the run
if ( $incoming_serial == ''){
// APP_ROOT is set in index.php
require_once(APP_ROOT.'vendor/munkireport/machine/machine_model.php');
// Get all the serial numbers in an object
$machinedata = Machine_model::selectRaw("machine.serial_number")->filter()->get()->toArray();
// Loop through each serial number for processing
$out = array();
foreach ($machinedata as $serialobj) {
$out[] = $serialobj['serial_number'];
}
$obj = new View();
$obj->view('json', array('msg' => $out));
} else {
$kandji = new Kandji_model();
$kandji->serial_number = $incoming_serial;
$kandji->kandji_id = 0;
$kandji_status = $this->run_kandji_stats($kandji);
// Check if machine exists in Kandji
if ($kandji->kandji_id == 0 ){
$out = array("serial"=>$incoming_serial,"status"=>"Machine not found in Kandji!");
} else {
$out = array("serial"=>$incoming_serial,"status"=>"Machine processed");
}
$obj = new View();
$obj->view('json', array('msg' => $out));
}
}
/**
* Get Kandji data
*
* @return void
* @author jc0b
**/
function run_kandji_stats(&$kandji_model)
{
$module_dir = dirname(__FILE__);
// Check if we should enable Kandji lookup
if (conf('kandji_enable')) {
// Load Kandji helper
require_once($module_dir.'/lib/kandji_helper.php');
$kandji_helper = new munkireport\module\kandji\kandji_helper;
$kandji_helper->pull_kandji_data($kandji_model);
// ^^ Comment and uncomment to turn off and on
}
return $this;
}
/**
* Force data pull from Kandji
*
* @return void
* @author tuxudo
**/
public function recheck_kandji($serial = '')
{
if (authorized_for_serial($serial)) {
$kandji = new Kandji_model();
$kandji->serial_number = $serial;
$this->run_kandji_stats($kandji);
}
redirect("clients/detail/$serial#tab_kandji-tab");
}
/**
* Get Kandji information for serial_number
*
* @param string $serial serial number
**/
public function get_data($serial_number = '')
{
$machinedata = Kandji_model::select("kandji.*")->where("kandji.serial_number", $serial_number)->filter()->get();
$obj = new View();
$obj->view('json', array('msg' => $machinedata[0]));
}
} // End class kandji_module