forked from remyd1/salt_states
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonreader.php
More file actions
260 lines (249 loc) · 10.1 KB
/
jsonreader.php
File metadata and controls
260 lines (249 loc) · 10.1 KB
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
<html>
<head>
<meta charset="utf-8">
<title>Salt Json Monitor</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="//cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="css/jsonreader.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="//cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<script>
$( document ).ready(function() {
<?php
$curYYMMDD = date("Ymd");
$curYYMM = date("Ym");
if(isset($_POST["datechoose"]) && isset($_POST["data"])) {
$dateChoosen = $_POST["datechoose"];
$data = $_POST["data"];
$filename = "./" . $curYYMM . "/" . $dateChoosen . $data . ".json";
$string = file_get_contents($filename);
$json = json_decode($string, true);
switch (json_last_error()) {
//check json content
case JSON_ERROR_NONE:
echo 'console.log("Aucune erreur");'."\n";
break;
case JSON_ERROR_DEPTH:
echo 'console.log("Profondeur maximale atteinte");'."\n";
break;
case JSON_ERROR_STATE_MISMATCH:
echo 'console.log("Inadéquation des modes ou underflow");'."\n";
break;
case JSON_ERROR_CTRL_CHAR:
echo 'console.log("Erreur lors du contrôle des caractères");'."\n";
break;
case JSON_ERROR_SYNTAX:
echo 'console.log("Erreur de syntaxe ; JSON malformé");'."\n";
break;
case JSON_ERROR_UTF8:
echo 'console.log("Caractères UTF-8 malformés, probablement une erreur d\'encodage");'."\n";
break;
default:
echo 'console.log("Erreur inconnue");'."\n";
break;
}
//var_dump($json);
$disks = false;
//thead
echo "var thead = $('thead');"."\n";
if($data == "_hosts_status") {
echo "thead.append('<tr><th>Server</th><th>Status</th></tr>')"."\n";
}
elseif($data == "_services") {
//echo "thead.append('<tr><th>Server</th><th>Servicename</th><th>Status</th><th>Comment</th><th>Start</th></tr>')"."\n";
echo "thead.append('<tr><th>Server</th><th>Command Result</th><th>Service</th><th>Status</th></tr>')"."\n";
}
elseif($data == "_disks") {
echo "thead.append('<tr><th>Server</th><th>Mountpoint</th><th>Usage [%]</th></tr>')"."\n";
}
else {
echo "console.log('Data type unknown.');"."\n";
}
// tbody
echo "var tbody = $('tbody');"."\n";
foreach ($json as $name => $val) {
if(!is_array($json[$name])) {
/* hosts state */
echo 'var tr = $("<tr>");'."\n";
echo "$('<td>').html('".$name."').appendTo(tr);"."\n";
if ( $val == null) {
$val = "0";
}
echo "$('<td>').html('".$val."').appendTo(tr);"."\n";
echo 'tbody.append(tr);'."\n";
} else {
$hostArray = $json[$name];
foreach ($hostArray as $subname => $subval) {
if(is_array($hostArray[$subname]) && isset($hostArray[$subname])) {
/* service / daemon state */
$servArray = $hostArray[$subname];
echo 'var tr = $("<tr>");'."\n";
echo "$('<td>').html('".$name."').appendTo(tr);"."\n";
$pgrep = False;
foreach ($servArray as $daemon_type => $daemonval) {
if($daemon_type == "name" && $daemonval != "customservice.status") {
$html_str_name = "<td>".$daemonval."</td>";
$pgrep = True;
}
elseif($daemon_type == "result") {
if ( $daemonval == null) {
$daemonval = "0";
}
$html_str_result = "<td>".$daemonval."</td>";
}
/*elseif($daemon_type == "comment") {
$html_str_comment = "<td>".$daemonval."</td>";
}*/
elseif($daemon_type == "changes") {
if(array_key_exists("ret", $daemonval)) {
$ret_daemonval = $daemonval["ret"];
$html_str_changes = "<td>".$ret_daemonval[0]."</td>";
$html_str_changes .= "<td>".$ret_daemonval[1]."</td>";
}
elseif(array_key_exists("stdout", $daemonval)) {
$ret_daemonval = $daemonval["stdout"];
$html_str_changes = "<td>".$ret_daemonval."</td>";
}
}
else {
if($pgrep != True) {
$html_str_name = "";
}
}
}
$tdhtml = $html_str_result.$html_str_name.$html_str_changes;
echo 'tr.append("'.$tdhtml.'");'."\n";
echo 'tbody.append(tr);'."\n";
//reinitialize
$tdhtml = "";
$html_str_name = "";
$pgrep = False;
$html_str_result = "";
$html_str_changes = "";
}
else {
echo 'var tr = $("<tr>");'."\n";
echo "$('<td>').html('".$name."').appendTo(tr);"."\n";
echo "$('<td>').html('".$subname."').appendTo(tr);"."\n";
echo "$('<td>').html('".$subval."').appendTo(tr);"."\n";
echo 'tbody.append(tr);'."\n";
}
}
}
}
echo "\n";
echo "$('#datechoose').val('".$dateChoosen."');"."\n";
}
else {
$data = "_hosts_status";
echo '$("#datechoose").datepicker({dateFormat: "yymmdd"}).datepicker("setDate", new Date());'."\n";
}
?>
$( "#datechoose" ).datepicker({
dateFormat: "yymmdd",
defaultDate: null,
});
$('#myTable').DataTable({
//"lengthChange": false,
"lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "All"] ],
<?php if($data == "_hosts_status") { ?>
"pageLength": -1,
"order": [[ 1, "desc" ], [0, "asc"]],
"createdRow": function( row, data, dataIndex ) {
if ( data[1] == "1" ) {
/* Server status */
$('td', row).eq(0).addClass('true');
$('td', row).eq(1).addClass('true');
}
else {
/* Server is down or does not respond ! */
$('td', row).eq(0).addClass('false');
$('td', row).eq(1).addClass('false');
}
}
<?php } elseif($data == "_services") { ?>
"pageLength": -1,
"order": [[ 3, "asc" ], [0, "asc"], [2, "asc"]],
"createdRow": function( row, data, dataIndex ) {
if ( data[2].indexOf("pgrep") != -1) {
var PID = data[3];
if (!isNaN(parseFloat(PID)) && isFinite(PID)) {
$('td', row).addClass('true');
}
else {
$('td', row).addClass('false');
}
}
else {
if ( data[3] == "1" ) {
/* Service / daemon status is ok */
$('td', row).addClass('true');
}
else {
/* Service / daemon status is down */
$('td', row).addClass('false');
/*
if ( data[3].indexOf("has been enabled") != -1 ) {
$('td', row).eq(3).addClass('warning');
} else {
$('td', row).eq(3).addClass('true');
}
*/
}
}
}
<?php } elseif($data == "_disks") { ?>
"pageLength": 25,
"order": [[ 2, "desc" ], [0, "asc"]],
"createdRow": function( row, data, dataIndex ) {
var x = data[2].replace(/%/, "");
x = parseFloat(x);
if ( x > "90" ) {
/* Service / daemon status is ok */
$('td', row).eq(0).addClass('false');
$('td', row).eq(1).addClass('false');
$('td', row).eq(2).addClass('false');
}
else {
/* Service / daemon status is down */
$('td', row).eq(0).addClass('true');
$('td', row).eq(1).addClass('true');
$('td', row).eq(2).addClass('true');
}
}
<?php } else {} ?>
});
});
</script>
</head>
<body>
<form method='POST'>
<br />
<br />
Select date [YYYYMMDD] : <input type="text" id="datechoose" name="datechoose" />
<br />
<br />
Select Data :
<input type="radio" name="data" value="_hosts_status" checked /> Server status
<input type="radio" name="data" value="_services" /> Daemon status
<input type="radio" name="data" value="_disks" /> Disk usage
<br />
<br />
<input type='submit' value='ok' id='subm' />
</form>
<div id="results">
<table id="myTable" class="perso compact cell-border">
<thead>
</thead>
<tbody></tbody>
</table>
</div>
</body>
</html>