forked from loadavg/loadavg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.php
More file actions
executable file
·257 lines (182 loc) · 6.33 KB
/
logger.php
File metadata and controls
executable file
·257 lines (182 loc) · 6.33 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
<?php
/**
* LoadAvg - Server Monitoring & Analytics
* http://www.loadavg.com
*
* Get and log data
*
* @link https://github.com/loadavg/loadavg
* @author Karsten Becker
* @copyright 2014 Sputnik7
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
// initialize LoadAvg and grab data
require_once dirname(__FILE__) . '/globals.php'; // including required globals
include 'class.Utility.php'; // for logger module
///////////////////////////////////////////////////////////////
//
//for testing the system timzeone info
$timezone = false;
if ( (defined('STDIN') && isset($argv[1]) && ($argv[1] == 'timezone')) ) {
$timezone = true;
}
if ($timezone) {
$systemTimeZone = exec('date +%Z');
echo 'Server time : '. $systemTimeZone ."\n";
//$timestamp = time();
//echo 'PHP time : ' . date("Y-m-d H:i:s", $timestamp ) . "\n";
echo 'PHP Core timezone : ' . date_default_timezone_get() . "\n";
}
//
/////////////////////////////////////////////////////////////
// include logger module
include 'class.Logger.php';
$logger = new Logger(); // Initializing Main Controller
if ($timezone)
echo 'Logger time: ' . date_default_timezone_get() ."\n";
// include timer module
include 'class.Timer.php';
$timer = new Timer(); // Initializing Timer
// include alerts module
include 'class.Alert.php';
$alert = new Alert(); // Initializing Alert
if (Logger::$_settings->general['settings']['logalerts'] == "true")
$alert->setStatus(true);
// Get list of modules and thier status
$loadedModules = Logger::$_modules;
//grab the log directory properly
//need to grab from system settings.ini instead
//$logdir = LoadAvg::$_settings->general['logs_dir']; // Loaded modules
$logdir = LOG_PATH;
//for testing the system
$testmode = false;
if ( (defined('STDIN') && isset($argv[1]) && ($argv[1] == 'status')) ) {
$testmode = true;
}
$timemode = false;
$st = $et = null;
if ( (defined('STDIN') && isset($argv[1]) && ($argv[1] == 'time')) ) {
$timemode = true;
$timer->setStartTime(); // Setting page load start time
echo "Start Time : " . $timer->timeStart . " \n";
}
//check for api server data transfer
$api = false;
if (Logger::$_settings->general['settings']['apiserver'] == "true") {
$api = true;
}
//array of data from logging used to send to api
$response = array();
////////////////////////////////////////////////
// Delete/rotate out old log files
//TODO: update for nested log folders!
$logger->rotateLogFiles($logdir);
//TODO when sending api data we call data gathering 2x this is unnecssary
//we only need to call 1x and return data as string or true/false
//we can add 3 different modes to caller
//disk - log data to disk, default
//apionly - send back for api only no logging
//api - log to disk and send back for api
$logMode = "disk";
//set api mode flag
if ( $api )
$logMode = "api";
if (!$testmode) {
if (LOGDEBUG) echo "Start Main LOOP \n";
//check to see if alerts module is loaded and if so initialize alert object
if ($alert->getStatus()) {
$alert->initializeAlerts();
if (LOGDEBUG) echo "ALERT LOGGING ON \n";
} else {
if (LOGDEBUG) echo "ALERT LOGGING OFF \n";
}
// Check for each module we have loaded
foreach ( $loadedModules as $module => $value ) {
if (LOGDEBUG) echo 'Module : ' . $module . ' module status ' . $value . "\n";
if ( $value == "false" )
continue;
// Settings for each loaded modules
$moduleSettings = Logger::$_settings->$module;
// Check if loaded module needs loggable capabilities
if ( $moduleSettings['module']['logable'] == "true" ) {
// load module information
$class = Logger::$_classes[$module];
// loop through module logging arguments
//multiple args mean multiple charts like mysql or network modules
foreach ( $moduleSettings['logging']['args'] as $args) {
$args = json_decode($args); // decode arguments
$class->logfile = $logdir . $args->logfile; // the modules logfile si read from args
//check for logdir
if ( isset($moduleSettings['module']['hasownlogdir']) &&
$moduleSettings['module']['hasownlogdir'] == "true" ) {
$class->logdir = $args->logdir; // the modules logdir as read from args
}
//see if we are timing, if so set start time
if ( $timemode )
$st = $timer->getTime();
//
//run modules logger
$responseData = $class->logData($logMode);
// if API then collect data for API server
if ( $api ) {
//TODO: nead a way to deal with modules that return more than one dataset for api
//this is for networking module
if (is_array($responseData))
{
$timestamp = "";
$dataInterface = "";
foreach ($responseData as $interface => $value) {
//echo 'INT: ' . $interface . ' VAL: ' . $value . "\n";
$data = explode("|", $value); // parsing response data
$dataInterface[$interface] = array("data" => $value, "timestamp" => $data[0]);
}
$response[$module] = $dataInterface;
} else {
$data = explode("|", $responseData); // parsing response data
$timestamp = $data[0];
$response[$module] = array("data" => $responseData, "timestamp" => $timestamp); // Populating response array
}
}
if ( $timemode ) {
$et = $timer->getTime();
echo "Module " . $module . " Time : " . ($et - $st) . " \n";
}
}
}
}
if ($alert->getStatus()) {
$alert->writeAlerts();
}
// Send data to API server
if ( $api ) {
//print_r($response) ;
$apistatus = $logger->sendApiData($response);
}
}
/*
* testing section
* used to test if logger is running
*
* EXECUTE: php logger.php status
*/
if ( $testmode ) {
echo "Testing Logger \n";
$logger->testLoggerCore($api);
}
/*
* timing section
* used to time logger
*
* EXECUTE: php logger.php time
*/
if ( $timemode ) {
$timer->setFinishTime(); // Setting page load finish time
$page_load = $timer->getPageLoadTime(); // Calculating page load time
$mytime = (float) $timer->timeFinish - (float) $timer->timeStart;
echo "End Time : " . $timer->timeFinish . " \n";
echo "Total Time : " . $mytime . " \n";
echo " : " . $page_load . " \n";
}
?>