1 | <?php |
||
2 | |||
3 | header('Content-type: application/json'); |
||
4 | |||
5 | if (! Auth::user()->hasGlobalAdmin()) { |
||
6 | $response = [ |
||
7 | 'status' => 'error', |
||
8 | 'message' => 'Need to be admin', |
||
9 | ]; |
||
10 | echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||
11 | exit; |
||
12 | } |
||
13 | |||
14 | $status = 'ok'; |
||
15 | $message = ''; |
||
16 | |||
17 | $device_id = $_POST['device_id']; |
||
18 | $id = $_POST['ccustomoid_id']; |
||
19 | $action = $_POST['action']; |
||
20 | $name = $_POST['name']; |
||
21 | $oid = $_POST['oid']; |
||
22 | $datatype = $_POST['datatype']; |
||
23 | if (empty(($_POST['unit']))) { |
||
24 | $unit = ['NULL']; |
||
25 | } else { |
||
26 | $unit = $_POST['unit']; |
||
27 | } |
||
28 | if (! empty(($_POST['limit'])) && is_numeric($_POST['limit'])) { |
||
29 | $limit = $_POST['limit']; |
||
30 | } else { |
||
31 | $limit = ['NULL']; |
||
32 | } |
||
33 | if (! empty(($_POST['limit_warn'])) && is_numeric($_POST['limit_warn'])) { |
||
34 | $limit_warn = $_POST['limit_warn']; |
||
35 | } else { |
||
36 | $limit_warn = ['NULL']; |
||
37 | } |
||
38 | if (! empty(($_POST['limit_low'])) && is_numeric($_POST['limit_low'])) { |
||
39 | $limit_low = $_POST['limit_low']; |
||
40 | } else { |
||
41 | $limit_low = ['NULL']; |
||
42 | } |
||
43 | if (! empty(($_POST['limit_low_warn'])) && is_numeric($_POST['limit_low_warn'])) { |
||
44 | $limit_low_warn = $_POST['limit_low_warn']; |
||
45 | } else { |
||
46 | $limit_low_warn = ['NULL']; |
||
47 | } |
||
48 | if ($_POST['alerts'] == 'on') { |
||
49 | $alerts = 1; |
||
50 | } else { |
||
51 | $alerts = 0; |
||
52 | } |
||
53 | if ($_POST['passed'] == 'on') { |
||
54 | $passed = 1; |
||
55 | } else { |
||
56 | $passed = 0; |
||
57 | } |
||
58 | if (! empty(($_POST['divisor'])) && is_numeric($_POST['divisor'])) { |
||
59 | $divisor = $_POST['divisor']; |
||
60 | } else { |
||
61 | $divisor = 1; |
||
62 | } |
||
63 | if (! empty(($_POST['multiplier'])) && is_numeric($_POST['multiplier'])) { |
||
64 | $multiplier = $_POST['multiplier']; |
||
65 | } else { |
||
66 | $multiplier = 1; |
||
67 | } |
||
68 | if (! empty(($_POST['user_func']))) { |
||
69 | $user_func = $_POST['user_func']; |
||
70 | } else { |
||
71 | $user_func = ['NULL']; |
||
72 | } |
||
73 | |||
74 | if ($action == 'test') { |
||
75 | $query = 'SELECT * FROM `devices` WHERE `device_id` = ? LIMIT 1'; |
||
76 | $device = dbFetchRow($query, [$device_id]); |
||
77 | |||
78 | $rawdata = snmp_get($device, $oid, '-Oqv'); |
||
79 | |||
80 | if (is_numeric($rawdata)) { |
||
81 | if (dbUpdate( |
||
82 | [ |
||
83 | 'customoid_passed' => 1, |
||
84 | ], |
||
85 | 'customoids', |
||
86 | 'customoid_id=?', |
||
87 | [$id] |
||
88 | ) >= 0) { |
||
89 | $message = "Test successful for <i>$name</i>, value $rawdata received"; |
||
90 | } else { |
||
91 | $status = 'error'; |
||
92 | $message = "Failed to set pass on OID <i>$name</i>"; |
||
93 | } |
||
94 | } else { |
||
95 | $status = 'error'; |
||
96 | $message = "Invalid data in SNMP reply, value $rawdata received"; |
||
97 | } |
||
98 | } else { |
||
99 | if (is_numeric($id) && $id > 0) { |
||
100 | if (dbUpdate( |
||
101 | [ |
||
102 | 'customoid_descr' => $name, |
||
103 | 'customoid_oid' => $oid, |
||
104 | 'customoid_datatype' => $datatype, |
||
105 | 'customoid_unit' => $unit, |
||
106 | 'customoid_divisor' => $divisor, |
||
107 | 'customoid_multiplier' => $multiplier, |
||
108 | 'customoid_limit' => $limit, |
||
109 | 'customoid_limit_warn' => $limit_warn, |
||
110 | 'customoid_limit_low' => $limit_low, |
||
111 | 'customoid_limit_low_warn' => $limit_low_warn, |
||
112 | 'customoid_alert' => $alerts, |
||
113 | 'customoid_passed' => $passed, |
||
114 | 'user_func' => $user_func, |
||
115 | ], |
||
116 | 'customoids', |
||
117 | '`customoid_id` = ?', |
||
118 | [$id] |
||
119 | ) >= 0) { //end if condition |
||
120 | $message = "Edited OID: <i>$name</i>"; |
||
121 | } else { |
||
122 | $status = 'error'; |
||
123 | $message = "Failed to edit OID <i>$name</i>"; |
||
124 | } |
||
125 | } else { |
||
126 | if (empty($name)) { |
||
127 | $status = 'error'; |
||
128 | $message = 'No OID name provided'; |
||
129 | } else { |
||
130 | if (dbFetchCell('SELECT 1 FROM `customoids` WHERE `customoid_descr` = ? AND `device_id`=?', [$name, $device_id])) { |
||
131 | $status = 'error'; |
||
132 | $message = "OID named <i>$name</i> on this device already exists"; |
||
133 | } else { |
||
134 | $id = dbInsert( |
||
135 | [ |
||
136 | 'device_id' => $device_id, |
||
137 | 'customoid_descr' => $name, |
||
138 | 'customoid_oid' => $oid, |
||
139 | 'customoid_datatype' => $datatype, |
||
140 | 'customoid_unit' => $unit, |
||
141 | 'customoid_divisor' => $divisor, |
||
142 | 'customoid_multiplier' => $multiplier, |
||
143 | 'customoid_limit' => $limit, |
||
144 | 'customoid_limit_warn' => $limit_warn, |
||
145 | 'customoid_limit_low' => $limit_low, |
||
146 | 'customoid_limit_low_warn' => $limit_low_warn, |
||
147 | 'customoid_alert' => $alerts, |
||
148 | 'customoid_passed' => $passed, |
||
149 | 'user_func' => $user_func, |
||
150 | ], |
||
151 | 'customoids' |
||
152 | ); |
||
153 | if ($id) { |
||
0 ignored issues
–
show
|
|||
154 | $message = "Added OID: <i>$name</i>"; |
||
155 | } else { |
||
156 | $status = 'error'; |
||
157 | $message = "Failed to add OID: <i>$name</i>"; |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | exit(json_encode([ |
||
165 | 'status' => $status, |
||
166 | 'message' => $message, |
||
167 | ])); |
||
168 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: