-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlaunch.php
More file actions
191 lines (165 loc) · 5.19 KB
/
launch.php
File metadata and controls
191 lines (165 loc) · 5.19 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
<?php
// CLI only
if (php_sapi_name() !== 'cli')
die('Access Denied');
define('VERSION', "1.0.0");
// Class load
require __DIR__. '/src/GetOpt.php';
// Config file
$config = require __DIR__ . '/config.inc.php';
$dbConfig = & $config['database'];
$mtrConfig = & $config['mtr'];
$apiConfig = & $config['api'];
$config = $config['general'];
// Timezone setting
date_default_timezone_set($config['timezone']);
/**
* Options definition
*/
$shortopts = "";
$shortopts .= "h:";
$shortopts .= "m::";
$shortopts .= "p::";
$shortopts .= "c::";
$shortopts .= "T";
$shortopts .= "P::";
$shortopts .= "v";
// Long options
$longopts = array(
"category::",
"host:",
"mtr-arg::",
"period::",
"report-cycles::",
"tcp",
"port::",
"debug",
"help",
"version",
);
// GetOpt
$getOpt = new GetOpt($shortopts, $longopts);
// var_dump($getOpt->getOptions());exit;
$mtr = [];
$mtr['mtrArgv'] = $getOpt->get(['mtr-arg', 'm']);
// Category
$optTmp = $getOpt->get(['category']);
$category = ($optTmp) ? $optTmp : $config['category'];
// Host
$optTmp = $getOpt->get(['host', 'h']);
$mtr['host'] = ($optTmp) ? $optTmp : $mtrConfig['host'];
// Period
$optTmp = $getOpt->get(['period', 'p']);
$mtr['period'] = ($optTmp) ? $optTmp : $mtrConfig['period'];
// Count
$optTmp = $getOpt->get(['report-cycles', 'c']);
$mtr['cycles'] = ($optTmp) ? $optTmp : $mtrConfig['count'];
// TCP
$optTmp = $getOpt->has(['tcp', 'T']);
$mtr['tcp-cmd'] = ($optTmp || $mtrConfig['tcp']) ? '--tcp' : '';
// TCP port
$optTmp = $getOpt->get(['port', 'P']);
$mtr['port'] = ($optTmp) ? $optTmp : $mtrConfig['port'];
$mtr['port-cmd'] = ($mtr['tcp-cmd']) ? "--port={$mtr['port']}" : '';
// Others
$debugMode = $getOpt->has(['debug']);
$showHelp = $getOpt->has(['help']);
$showVersion = $getOpt->has(['version']);
if ($showHelp) {
exit;
}
elseif ($showVersion) {
exit(VERSION . "\n");
}
// MTR process
$startDateTime = date("Y-m-d H:i:s");
$interval = floor(($mtr['period'] * 60) / $mtr['cycles']);
$cmd = "{$config['mtrCmd']} {$mtr['host']} -c {$mtr['cycles']} -i {$interval} {$mtr['tcp-cmd']} {$mtr['port-cmd']} -rb --json {$mtr['mtrArgv']}";
// $cmd = "{$config['mtrCmd']} -rb -c 3 -i 1 --json google.com";
if ($debugMode) {
echo "{$cmd}\n";exit;
}
$output = shell_exec($cmd);
$endDateTime = date("Y-m-d H:i:s");
// echo $output;exit;
$data = json_decode($output, true);
// var_dump($data);exit;
// Check JSON
if (!isset($data['report'])) {
die("Error!: MTR output result is wrong");
}
// Get end hub
$endHub = end($data['report']['hubs']);
// Save to database
$insertMap = [
'start_datetime' => $startDateTime,
'end_datetime' => $endDateTime,
'category' => $category,
'source' => $data['report']['mtr']['src'],
'destination' => $data['report']['mtr']['dst'],
'period' => $mtr['period'],
'host' => $endHub['host'],
'mtr_loss' => $endHub['Loss%'],
'mtr_sent' => $endHub['Snt'],
'mtr_avg' => $endHub['Avg'],
'mtr_best' => $endHub['Best'],
'mtr_worst' => $endHub['Wrst'],
'mtr_raw' => $output,
'command' => $cmd,
];
if ($apiConfig['agent']['enabled']) {
// API method
$data = [
'key' => $apiConfig['key'],
'data' => $insertMap,
];
$postData = json_encode($data);
// Initialize curl
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $apiConfig['agent']['reportUrl']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($postData))
);
// Execute post
$responseStream = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close connection
curl_close($ch);
// Get response body
$result = json_decode($responseStream, true);
// Check response
if ($httpCode == 500) {
die("Error: {$result['message']} \n");
}
elseif ($httpCode != 200) {
die("Error: Colletor API respond with HTTP code: {$httpCode} \n");
}
} else {
// Database connection
try {
// Database connection
$conn = new PDO("mysql:host={$dbConfig['host']};dbname={$dbConfig['database']}", $dbConfig['username'], $dbConfig['password']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
} catch (PDOException $e) {
die("Error!: " . $e->getMessage() . "\n");
}
// Database method
$sql = "INSERT INTO {$dbConfig['table']} (sn, start_datetime, end_datetime, period, category, source, destination, host, mtr_loss, mtr_sent, mtr_avg, mtr_best, mtr_worst, mtr_raw, command)
VALUES (NULL, :start_datetime, :end_datetime, :period, :category, :source, :destination, :host, :mtr_loss, :mtr_sent, :mtr_avg, :mtr_best, :mtr_worst, :mtr_raw, :command)";
$stmt = $conn->prepare($sql);
foreach ($insertMap as $key => $value) {
$stmt->bindValue(":{$key}", $value);
}
$result = $stmt->execute();
if ($result === false) {
// $stmt->debugDumpParams();
die("Error!: " . $stmt->errorInfo()[2] . "\n");
}
}
exit("Process success\n");