forked from loadavg/loadavg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.Mysql.php
executable file
·274 lines (208 loc) · 5.69 KB
/
class.Mysql.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
<?php
/**
* LoadAvg - Server Monitoring & Analytics
* http://www.loadavg.com
*
* Memory Module for LoadAvg
*
* @version SVN: $Id$
* @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.
*/
class Mysql extends Charts
{
/**
* __construct
*
* Class constructor, appends Module settings to default settings
*
*/
public function __construct()
{
$this->setSettings(__CLASS__, parse_ini_file(strtolower(__CLASS__) . '.ini.php', true));
}
/**
* getChartLabel
*
* Gets label for chart based on mode
*
* @return the label for the mode
*
*/
public function getChartLabel( $switch )
{
//mode specific data is set up here
//1 == Transmit
//2 == Receive
//3 == Queries
$theLabel = "";
switch ( $switch) {
case 1: $theLabel = "Transmit";
break;
case 2: $theLabel = "Receive";
break;
case 3: $theLabel = "Queries";
break;
}
return $theLabel;
}
/**
* getData
*
* Gets data from logfile, formats and parses it to pass it to the chart generating function
*
* @return array $return data retrived from logfile
*
*/
public function getData( $switch = 1)
{
$class = __CLASS__;
$settings = loadModules::$_settings->$class;
//define some core variables here
$dataArray = $dataRedline = $usage = array();
$dataArraySwap = array();
//display switch used to switch between view modes - data or percentage
//$displayMode = $settings['settings']['display_limiting'];
//mode specific data is set up here
$dataArrayLabel[0] = $this->getChartLabel($switch);
/*
* grab the log file data needed for the charts as array of strings
* takes logfiles(s) and gives us back contents
*/
$contents = array();
$logStatus = LoadUtility::parseLogFileData($this->logfile, $contents);
/*
* build the chartArray array here as array of arrays needed for charting
* takes in contents and gives us back chartArray
*/
$chartArray = array();
$sizeofChartArray = 0;
if ($logStatus) {
//takes the log file and parses it into chartable data
$this->getChartData ($chartArray, $contents );
$sizeofChartArray = (int)count($chartArray);
}
/*
* now we loop through the dataset and build the chart
* uses chartArray which contains the dataset to be charted
*/
if ( $sizeofChartArray > 0 ) {
// main loop to build the chart data
for ( $i = 0; $i < $sizeofChartArray; ++$i) {
$data = $chartArray[$i];
if ($data==null)
continue;
//check for redline
$redline = false;
if ( isset ($data['redline']) && $data['redline'] == true )
$redline = true;
//when showing send and receive its bytes to MB
//when showing queries, mode 3, its 1 to 1
if ($switch == 3)
$divisor = 1;
else
$divisor = 1024;
//used to filter out redline data from usage data as it skews it
if (!$redline) {
$usage[] = ( $data[$switch] / $divisor );
}
$timedata = (int)$data[0];
$time[( $data[$switch] / $divisor )] = date("H:ia", $timedata);
$usageCount[] = ($data[0]*1000);
// received
$dataArray[0][$data[0]] = "[". ($data[0]*1000) .", ". ( $data[$switch] / $divisor ) ."]";
}
/*
* now we collect data used to build the chart legend
*
*/
$mysql_high = max($usage);
$mysql_low = min($usage);
$mysql_mean = array_sum($usage) / count($usage);
$ymax = $mysql_high;
$ymin = $mysql_low;
$mysql_high_time = $time[max($usage)];
$mysql_low_time = $time[min($usage)];
$mysql_latest = ( ( $usage[count($usage)-1] ) ) ;
// values used to draw the legend
$variables = array(
'mysql_high' => number_format($mysql_high,0),
'mysql_high_time' => $mysql_high_time,
'mysql_low' => number_format($mysql_low,0),
'mysql_low_time' => $mysql_low_time,
'mysql_mean' => number_format($mysql_mean,0),
'mysql_latest' => number_format($mysql_latest,0),
);
/*
* all data to be charted is now cooalated into $return
* and is returned to be charted
*
*/
$return = array();
// get legend layout from ini file
$return = $this->parseInfo($settings['info']['line'], $variables, __CLASS__);
//parse, clean and sort data
$depth=1; //number of datasets
$this->buildChartDataset($dataArray,$depth);
$return['chart'] = array(
'chart_format' => 'line',
'chart_avg' => 'avg',
'ymin' => $ymin,
'ymax' => $ymax,
'xmin' => date("Y/m/d 00:00:01"),
'xmax' => date("Y/m/d 23:59:59"),
'mean' => $mysql_mean,
'avg' => "stack",
'dataset' => $dataArray,
'dataset_labels' => $dataArrayLabel
//'overload' => $settings['settings']['overload']
);
return $return;
} else {
return false;
}
}
/**
* getTransferData
*
* Gets data from logfile, formats and parses it to pass it to the chart generating function
*
* @return array $return data retrived from logfile
*
*/
public function getTransferData( )
{
$returnStatus = $this->getData( 1 );
return $returnStatus;
}
/**
* getTransferData
*
* Gets data from logfile, formats and parses it to pass it to the chart generating function
*
* @return array $return data retrived from logfile
*
*/
public function getReceiveData( )
{
$returnStatus = $this->getData( 2 );
return $returnStatus;
}
/**
* getTransferData
*
* Gets data from logfile, formats and parses it to pass it to the chart generating function
*
* @return array $return data retrived from logfile
*
*/
public function getQueryData( )
{
$returnStatus = $this->getData( 3 );
return $returnStatus;
}
}