forked from loadavg/loadavg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.Mysql.php
executable file
·185 lines (133 loc) · 4.96 KB
/
log.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
<?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 Logger
{
/**
* __construct
*
* Class constructor, appends Module settings to default settings
*
*/
public function __construct()
{
$this->setSettings(__CLASS__, parse_ini_file(strtolower(__CLASS__) . '.ini.php', true));
}
/**
* logMemoryUsageData
*
* Retrives data and logs it to file
*
* @param string $type type of logging default set to normal but it can be API too.
* @return string $string if type is API returns data as string
*
*/
public function logData( $type = false )
{
$class = __CLASS__;
$settings = Logger::$_settings->$class;
//get database settings
//need some error checking here ie return if they are empty
$mysqlserver = $settings['settings']['mysqlserver'];
$mysqluser = $settings['settings']['mysqluser'];
$mysqlpassword = $settings['settings']['mysqlpassword'];
//test database connection
$connection = mysqli_connect($mysqlserver,$mysqluser,$mysqlpassword);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return false;
}
$query1 = mysqli_query($connection, "SHOW GLOBAL STATUS LIKE 'Bytes_received'") ;
$query2 = mysqli_query($connection, "SHOW GLOBAL STATUS LIKE 'Bytes_sent'") ;
$query3 = mysqli_query($connection, "SHOW GLOBAL STATUS LIKE 'Queries'") ;
//write the results
$row = mysqli_fetch_array($query1);
$bytesReceived = $row[1];
$row = mysqli_fetch_array($query2);
$bytesSent = $row[1];
$row = mysqli_fetch_array($query3);
$queries = $row[1];
//free up querys and connections
mysqli_free_result($query1);
mysqli_free_result($query2);
mysqli_free_result($query3);
mysqli_close($connection);
//for debugging
//echo 'DATA READ : ' . $bytesReceived . '|' . $bytesSent . '|' . $queries . "\n";
//grab the logfile
$logfile = sprintf($this->logfile, date('Y-m-d'));
if ( $logfile && file_exists($logfile) )
$elapsed = time() - filemtime($logfile);
else
$elapsed = 0; //meaning new logfile
//used to help calculate the difference as mysql charts is thruput not value based
//this data is stored in _mysql_latest
// grab net latest location and figure out elapsed
$mysqllatestElapsed = 0;
$mysqlLatestLocation = dirname($logfile) . DIRECTORY_SEPARATOR . '_mysql_latest';
// basically if mysqllatestElapsed is within reasonable limits (logger interval + 20%) then its from the day
// before rollover so we can use it to replace regular elapsed
// which is 0 when there is anew log file
$last = null;
if (file_exists( $mysqlLatestLocation )) {
$last = explode("|", file_get_contents( $mysqlLatestLocation ) );
if ( ( !isset($last[1]) || !$last[1]) || ($last[1] == null) || ($last[1] == "") )
$last[1]=0;
if ( ( !isset($last[2]) || !$last[2]) || ($last[2] == null) || ($last[2] == "") )
$last[2]=0;
$mysqllatestElapsed = ( time() - filemtime($mysqlLatestLocation));
//if its a new logfile check to see if there is previous netlatest data
if ($elapsed == 0) {
//data needs to within the logging period limits to be accurate
$interval = $this->getLoggerInterval();
if (!$interval)
$interval = 360;
else
$interval = $interval * 1.2;
if ( $mysqllatestElapsed <= $interval )
$elapsed = $mysqllatestElapsed;
}
}
//echo 'LAST STORED : ' . $last[0] . '|' . $last[1] . '|' . $last[2] . "\n";
//if we were able to get last data from mysql latest above
//figure out the difference as thats what we chart
if (@$last && $elapsed) {
$recv_diff = ($bytesReceived - $last[0]) ;
if ($recv_diff < 0) $recv_diff = 0;
$sent_diff = ($bytesSent - $last[1]) ;
if ($sent_diff < 0) $sent_diff = 0;
//$queries_diff = ($queries - $last[2]) - 4; // we are the 4 queries! remove to be accurate really
$queries_diff = ($queries - $last[2]) ;
if ($queries_diff < 0) $queries_diff = 0 ;
$string = time() . "|" . $recv_diff . "|" . $sent_diff . "|" . $queries_diff . "\n";
//echo 'DATA WRITE : ' . $recv_diff . '|' . $sent_diff . '|' . $queries_diff . "\n";
} else {
//if this is the first value in the set and there is no previous data then its null
$lastlogdata = "|0.0|0.0|0.0";
$string = time() . $lastlogdata . "\n" ;
}
//write out log data here
LoadUtility::safefilerewrite($logfile,$string,"a",true);
// write out last transfare and received bytes to latest
$last_string = $bytesReceived."|".$bytesSent."|".$queries;
$fh = dirname($this->logfile) . DIRECTORY_SEPARATOR . "_mysql_latest";
LoadUtility::safefilerewrite($fh,$last_string,"w",true);
if ( $type == "api")
return $string;
else
return true;
}
}