-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.php
372 lines (370 loc) · 18.2 KB
/
api.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
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
<?php
//This file is the API File which handle all the valainet work
//Refer documentation for more information on how to extend the functionality of the software.
//Copyright (C) 2020-2021 valainet. All Rights Reserved.
//Developed by Aravindh
//API security level update 100220210940pm
//Import khatral DB access file
include 'classes/khatral.php';
//Insert to action table
include 'valai.php';
//if the action variable is set then proceed otherwise display a message
$auth = "";
session_start();
//API key authorization
if(isset($_POST['apikey'])){
$ret = khatral::khquery('SELECT * FROM api_key WHERE api_key_hash=:keyhash', array(
':keyhash'=>$_POST['apikey']
));
$authed = 0;
foreach($ret as $p){
$authed += 1;
}
if($authed >= 1){
$auth = "success";
}else{
$auth = "failure";
}
}
if(isset($_POST['inbox'])){
if($_POST['inbox'] == "influx"){
$auth = "success";
}else{
$auth = "failure";
}
}
//Once apikey is authenticated
if($auth == "success"){
if(isset($_POST['act'])){
if($_POST['act'] == "insert"){
valai::InsertToAct($_POST['ip'], $_POST['nm'], $_POST['stat']);
echo $_POST['stat'];
}else if($_POST['act'] == "ret"){
$ret = khatral::khquery('SELECT * FROM actions WHERE act_ip=:ip', array(
':ip'=>$_POST['ip']
));
foreach($ret as $p){
echo $p['act_nm'];
}
}else if($_POST['act'] == "del"){
valai::DeleteFromAct($_POST['ip']);
echo 'success';
}else if($_POST['act'] == 'insertstat'){
$ret = khatral::khquery('SELECT * FROM stat WHERE act_ip=:ip AND act_group=:group', array(':ip'=>$_POST['ip'], ':group'=>$_POST['group']));
$count = 0;
foreach($ret as $p){
$count += 1;
}
// echo $count;
if($count > 0){
valai::UpdateStat($_POST['ip'], $_POST['group'], $_POST['stat']);
}else{
valai::InsertToStat($_POST['ip'], $_POST['group'], $_POST['nm'], $_POST['stat']);
}
echo $_POST['stat'];
}else if($_POST['act'] == 'updatestat'){
valai::UpdateStat($_POST['ip'], $_POST['group'], $_POST['stat']);
}else if($_POST['act'] == 'updatestatx'){
if(isset($_SESSION['unme'])){
valai::UpdateStatusFail();
echo 'success';
}else{
valai::DisplayError();
}
}else if($_POST['act'] == 'retstat'){
if(isset($_SESSION['unme'])){
$ret = khatral::khquery('SELECT * FROM stat WHERE act_ip=:ip AND act_group=:group', array(
':ip'=>$_POST['ip'],
':group'=>$_POST['group']
));
foreach($ret as $p){
// echo 'success112';
echo $p['act_stat'];
}
}else{
valai::DisplayError();
}
}else if($_POST['act'] == "retstatx"){
if(isset($_SESSION['unme'])){
$res = khatral::khquerypar('SELECT * FROM comp_group');
foreach($res as $pi){
echo '<h4>Collection : ' . $pi['group_nm'] . '</h4>';
$ret = khatral::khquery('SELECT * FROM stat WHERE act_group=:group', array(':group'=>$pi['group_nm']));
echo '<div class="table-responsive">';
echo '<table class="table"><tr class="bg-dark text-white"><th>Status</th><th style="width: 30%;">IP</th><th style="width: 30%;">Asset Code</th><th style="width: 30%;">Computer Name</th><th>Actions</th></tr>';
$count = 0;
foreach($ret as $p){
$count += 1;
$rex = khatral::khquery('SELECT * FROM comp_info WHERE comp_ip=:ip AND comp_group=:group', array(
':ip'=>$p['act_ip'],
':group'=>$p['act_group']
));
$assetcode = '';
foreach($rex as $px){
$assetcode = $px['comp_asset_tag'];
}
if($p['act_stat'] == "success"){
echo '<tr><td class="" style="width: 8px;"><img src="/valainet/images/tick.png" style="width: 22px;"></td><td><a class="" href="basicinfo.php?ip=' . $p['act_ip'] . '&group=' . $p['act_group'] . '">' . $p['act_ip'] . '</a></td><td><a href="asset.php?id=' . $p['act_ip'] . '&group=' . $p['act_group'] . '&asset=' . $assetcode . '">' . $assetcode . '</a></td><td>' . $p['act_nm'] . '</td><td><a class="" href="basicinfo.php?ip=' . $p['act_ip'] . '&group=' . $p['act_group'] . '"><i class="far fa-eye"></i></a> <a href="alert.php?ip=' . $p['act_ip'] . '&group=' . $p['act_group'] . '"><i class="fas fa-bolt"></i></a></td></tr>';
}else{
echo '<tr><td class="" style="width: 8px;"><img src="/valainet/images/warning.svg" style="width: 20px;"></td><td><a class="" href="basicinfo.php?ip=' . $p['act_ip'] . '&group=' . $p['act_group'] . '">' . $p['act_ip'] . '</a></td><td><a href="asset.php?id=' . $p['act_ip'] . '&group=' . $p['act_group'] . '&asset=' . $assetcode . '">' . $assetcode . '</a></td><td>' . $p['act_nm'] . '</td><td><a class="" href="basicinfo.php?ip=' . $p['act_ip'] . '&group=' . $p['act_group'] . '"><i class="far fa-eye"></i></a> <a href="alert.php?ip=' . $p['act_ip'] . '&group=' . $p['act_group'] . '"><i class="fas fa-bolt"></i></a></td></tr>';
}
}
echo '</table>';
echo '</div>';
}
}else{
valai::DisplayError();
}
}else if($_POST['act'] == "retstatxuser"){
if(isset($_SESSION['unme'])){
$res = khatral::khquery('SELECT * FROM comp_group WHERE group_nm=:nm', array(':nm'=>$_POST['nm']));
foreach($res as $pi){
echo '<h4>Collection : ' . $pi['group_nm'] . '</h4>';
$ret = khatral::khquery('SELECT * FROM stat WHERE act_group=:group', array(':group'=>$pi['group_nm']));
echo '<div class="table-responsive">';
echo '<table class="table"><tr class="bg-dark text-white"><th>Status</th><th style="width: 30%;">IP</th><th style="width: 30%;">Asset Code</th><th style="width: 30%;">Computer Name</th><th>Actions</th></tr>';
$count = 0;
foreach($ret as $p){
$count += 1;
$rex = khatral::khquery('SELECT * FROM comp_info WHERE comp_ip=:ip AND comp_group=:group', array(
':ip'=>$p['act_ip'],
':group'=>$p['act_group']
));
$assetcode = '';
foreach($rex as $px){
$assetcode = $px['comp_asset_tag'];
}
if($p['act_stat'] == "success"){
echo '<tr><td class="" style="width: 8px;"><img src="/valainet/images/tick.png" style="width: 22px;"></td><td><a class="" href="basicinfo.php?ip=' . $p['act_ip'] . '&group=' . $p['act_group'] . '">' . $p['act_ip'] . '</a></td><td>' . $assetcode . '</td><td>' . $p['act_nm'] . '</td><td><a class="" href="basicinfo.php?ip=' . $p['act_ip'] . '&group=' . $p['act_group'] . '"><i class="far fa-eye"></i></a></td></tr>';
}else{
echo '<tr><td class="" style="width: 8px;"><img src="/valainet/images/warning.svg" style="width: 20px;"></td><td><a class="" href="basicinfo.php?ip=' . $p['act_ip'] . '&group=' . $p['act_group'] . '">' . $p['act_ip'] . '</a></td><td>' . $assetcode . '</td><td>' . $p['act_nm'] . '</td><td><a class="" href="basicinfo.php?ip=' . $p['act_ip'] . '&group=' . $p['act_group'] . '"><i class="far fa-eye"></i></a></td></tr>';
}
}
echo '</table>';
echo '</div>';
}
}else{
valai::DisplayError();
}
}else if($_POST['act'] == "insertinfo"){
$ret = khatral::khquery('SELECT * FROM comp_info WHERE comp_ip=:ip AND comp_group=:group', array(
':ip'=>$_POST['ip'],
':group'=>$_POST['group']
));
$count = 0;
foreach($ret as $p){
$count += 1;
}
if($count > 0){
valai::UpdateInfo($_POST['ip'], $_POST['group'], $_POST['asset'], $_POST['nm'], $_POST['os'], $_POST['proc'], $_POST['ram'], $_POST['dis']);
echo 'success';
}else{
valai::InsertInfo($_POST['ip'], $_POST['group'], $_POST['asset'], $_POST['nm'], $_POST['os'], $_POST['proc'], $_POST['ram'], $_POST['dis']);
echo 'success1';
}
}else if($_POST['act'] == "sayhello"){
echo 'hello';
}else if($_POST['act'] == "addgroup"){
valai::AddGroup($_POST['nm'], $_POST['descr']);
}else if($_POST['act'] == "rettot"){
if(isset($_SESSION['unme'])){
$ret = khatral::khquerypar('SELECT * FROM stat');
$active = 0;
$inactive = 0;
$tot = 0;
foreach($ret as $p){
if($p['act_stat'] == "success"){
$active += 1;
}else{
$inactive += 1;
}
$tot = $active + $inactive;
}
$responseVar = array(
'active'=>$active,
'inactive'=>$inactive
);
echo (json_encode($responseVar));
}else{
valai::DisplayError();
}
}else if($_POST['act'] == "deletestorage"){
valai::DeleteStorage($_POST['ip'], $_POST['group']);
}
else if($_POST['act'] == "insertstorage"){
valai::InsertStorage($_POST['ip'], $_POST['group'], $_POST['nm'], $_POST['tot'], $_POST['avl']);
}else if($_POST['act'] == "insertos"){
$ret = khatral::khquery('SELECT * FROM os WHERE os_ip=:ip AND os_group=:group', array(
':ip'=>$_POST['ip'],
':group'=>$_POST['group']
));
$count = 0;
foreach($ret as $p){
$count += 1;
}
if($count > 0){
valai::DeleteOs($_POST['ip'], $_POST['group']);
valai::AddOs($_POST['bit'], $_POST['serial'], $_POST['installed'], $_POST['uptime'], $_POST['booted'], $_POST['extip'], $_POST['ip'], $_POST['group']);
}else{
valai::AddOs($_POST['bit'], $_POST['serial'], $_POST['installed'], $_POST['uptime'], $_POST['booted'], $_POST['extip'], $_POST['ip'], $_POST['group']);
}
}else if($_POST['act'] == "retjson"){
$ret = khatral::khquery('SELECT * FROM process_moni WHERE moni_ip=:ip AND moni_group=:group', array(
':ip'=>$_POST['ip'],
':group'=>$_POST['group']
));
$count = 0;
$rows = array();
foreach($ret as $p){
${'file' . $count} = array("mm$count" => $p['moni_nm']);
$rows += ${'file' . $count};
$count += 1;
}
$rows += array("count" => $count);
echo (json_encode($rows));
}else if($_POST['act'] == "retjsonip"){
$ret = khatral::khquery('SELECT * FROM ip_addr_moni WHERE aip_ip=:ip AND aip_group=:group', array(
':ip'=>$_POST['ip'],
':group'=>$_POST['group']
));
$count = 0;
$rows = array();
foreach($ret as $p){
${'file' . $count} = array("mm$count" => $p['aip_nm']);
$rows += ${'file' . $count};
$count += 1;
}
$rows += array("count" => $count);
echo (json_encode($rows));
}else if($_POST['act'] == "updateprocstat"){
valai::UpdateProcessMoni($_POST['nm'], $_POST['stat'], $_POST['ip'], $_POST['group']);
$ret = khatral::khquery('SELECT * FROM alerts WHERE alert_nm=:nm AND alert_ip=:ip AND alert_group=:group', array(
':nm'=>$_POST['nm'],
':ip'=>$_POST['ip'],
':group'=>$_POST['group']
));
$count = 0;
$failure = 0;
$id = '';
foreach($ret as $p){
$count += 1;
$failure = $p['alert_stat'];
$id = $p['alert_id'];
}
if($count > 0){
if($failure==$_POST['stat']){
// echo 'failure';
// valai::UpdateAlerts($_POST['nm'], $_POST['mess'], $_POST['ip'], $_POST['group']);
}else{
if($_POST['stat'] != 'success'){
valai::InsertAlerts($_POST['nm'], "failure", $_POST['ip'], $_POST['group'], $_POST['stat']);
valai::insertDownRec($_POST['nm'], "failure", $_POST['ip'], $_POST['group']);
}else{
valai::DeleteAlerts($id);
}
// echo 'success';
}
}else{
if($_POST['stat'] != 'success'){
valai::InsertAlerts($_POST['nm'], "failure", $_POST['ip'], $_POST['group'], $_POST['stat']);
valai::insertDownRec($_POST['nm'], "failure", $_POST['ip'], $_POST['group']);
}
}
}else if($_POST['act'] == "updateipstat"){
valai::UpdateIndivIpStat($_POST['nm'], $_POST['stat'], $_POST['ip'], $_POST['group']);
}else if($_POST['act'] == "retipstatx"){
if(isset($_SESSION['unme'])){
$res = khatral::khquery('SELECT * FROM ip_addr_moni WHERE aip_ip=:ip AND aip_group=:group', array(
':ip'=>$_POST['ip'],
':group'=>$_POST['group']
));
echo '<div class="table-responsive">';
echo '<table class="table"><tr class=""><th style="width: 2px;">IP Address</th><th style="width: 30px;">Status</th></tr>';
$count = 0;
foreach($res as $p){
$count += 1;
if($p['aip_stat'] == "success"){
echo '<tr><td>' . $p['aip_nm'] . '</td><td style="width: 5px;"><img src="/valainet/images/tick.png" style="width: 22px;"></td></tr>';
}else{
echo '<tr><td>' . $p['aip_nm'] . '</td><td style="width: 5px;"><img src="/valainet/images/warning.svg" style="width: 20px;"></td></tr>';
}
}
}else{
valai::DisplayError();
}
}else if($_POST['act'] == "updateallip"){
if(isset($_SESSION['unme'])){
valai::UpdateAllIp();
echo 'success';
}else{
valai::DisplayError();
}
}
else if($_POST['act'] == "retprocstatx"){
if(isset($_SESSION['unme'])){
$res = khatral::khquery('SELECT * FROM process_moni WHERE moni_ip=:ip AND moni_group=:group', array(
':ip'=>$_POST['ip'],
':group'=>$_POST['group']
));
echo '<div class="table-responsive">';
echo '<table class="table"><tr class=""><th style="width: 2px;">Process Name</th><th style="width: 30px;">Status</th></tr>';
$count = 0;
foreach($res as $p){
$count += 1;
if($p['moni_stat'] == "success"){
echo '<tr><td>' . $p['moni_nm'] . '</td><td style="width: 5px;"><img src="/valainet/images/tick.png" style="width: 22px;"></td></tr>';
}else{
echo '<tr><td>' . $p['moni_nm'] . '</td><td style="width: 5px;"><img src="/valainet/images/warning.svg" style="width: 20px;"></td></tr>';
}
}
}else{
valai::DisplayError();
}
}else if($_POST['act'] == "updateallproc"){
if(isset($_SESSION['unme'])){
valai::UpdateAllProcessMoni();
echo 'success';
}else{
valai::DisplayError();
}
}else if($_POST['act'] == "updateprocess"){
valai::UpdateProcess($_POST['id'], $_POST['nm'], $_POST['mem'], $_POST['ip'], $_POST['group']);
echo 'success';
}else if($_POST['act'] == "deleteprocess"){
valai::DeleteProcess($_POST['ip'], $_POST['group']);
echo 'success1';
}else if($_POST['act'] == "retsamplejson"){
$ret = khatral::khquery('SELECT * FROM process_moni WHERE moni_ip=:ip AND moni_group=:group', array(
':ip'=>$_POST['ip'],
':group'=>$_POST['group']
));
$count = 0;
$rows = array();
foreach($ret as $p){
${'file' . $count} = array("mm$count" => $p['moni_nm']);
$rows += ${'file' . $count};
$count += 1;
}
$rows += array("count" => $count);
echo (json_encode($rows));
}else if($_POST['act'] == "retalertcount"){
$ret = khatral::khquerypar('SELECT COUNT(alert_id) AS total_alerts FROM alerts');
$count = 0;
foreach($ret as $p){
$count = $p['total_alerts'];
}
echo $count;
}
else{
echo '<img src="images/valaiweb.svg" style="width: 32px;"><h3>Valai API</h3><br />This is a api file and it will not execute on its own. <br />Please refer documentation for more information on how to access it and work with it. <br />Copyright © 2020 Valainet. All Rights Reserved.';
}
}else if(isset($_GET['act'])){
if($_GET['act'] == "sayhello"){
echo 'hello world';
}
}else{
echo 'failure';
}
}else{
valai::DisplayError();
}