Skip to content
This repository was archived by the owner on Jan 15, 2021. It is now read-only.

Commit 0644c65

Browse files
committed
Added "server-id" option to MySQL template, rewritten parse_cmdline() in ss_get_mysql_stats.php.
1 parent c247fe8 commit 0644c65

File tree

5 files changed

+55
-39
lines changed

5 files changed

+55
-39
lines changed

Changelog

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
2013-11-20: version 1.1
1+
2013-11-26: version 1.1
22
-------------------------
33
* Added MySQL template for Zabbix 2.0.x (first release)
4+
* Added FreeBSD support to Nagios plugins, partially rewritten pmp-check-unix-memory (bugs 1249575, 1244081)
5+
* Added new options to ss_get_mysql_stats.php to better support pt-heartbeat (bugs 1253125, 1253130)
46
* sql query for idle_blocker_duraction check in pmp-check-mysql-innodb did not conform sql mode of ONLY_FULL_GROUP_BY (bug #1240417)
57

68
2013-10-02: version 1.0.5

cacti/definitions/mysql.def

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3339,7 +3339,7 @@
33393339
'Get MySQL Stats' => {
33403340
hash => 'hash_03_VER_436e47b578d778e94d10a552d7d35bdf',
33413341
input_string =>
3342-
'<path_php_binary> -q <path_cacti>/scripts/ss_get_mysql_stats.php --host <hostname> --items <items> --user <username> --pass <password> --port <port>',
3342+
'<path_php_binary> -q <path_cacti>/scripts/ss_get_mysql_stats.php --host <hostname> --items <items> --user <username> --pass <password> --port <port> --server-id <server-id>',
33433343
inputs => [
33443344
{ allow_nulls => '',
33453345
hash => 'hash_07_VER_c8cef6437d22aa62790728f605a68d8e',
@@ -3357,6 +3357,10 @@
33573357
hash => 'hash_07_VER_0dde201d3dde810ddb9eeed6b444bf50',
33583358
name => 'password',
33593359
},
3360+
{ allow_nulls => 'on',
3361+
hash => 'hash_07_VER_5a1b802ba7e92609f8aa2b0521e221e1',
3362+
name => 'server-id'
3363+
},
33603364
],
33613365
outputs => {
33623366
Aborted_clients => 'hash_07_VER_89a607a1bcd6416e464ad38245fecfd8',

cacti/scripts/ss_get_mysql_stats.php

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ function array_change_key_case($arr) {
166166
function validate_options($options) {
167167
debug($options);
168168
$opts = array('host', 'items', 'user', 'pass', 'nocache', 'port', 'server-id');
169+
# Show help
170+
if ( array_key_exists('help', $options) ) {
171+
usage('');
172+
}
173+
169174
# Required command-line options
170175
foreach ( array('host', 'items') as $option ) {
171176
if ( !isset($options[$option]) || !$options[$option] ) {
@@ -190,12 +195,13 @@ function usage($message) {
190195
Usage: php ss_get_mysql_stats.php --host <host> --items <item,...> [OPTION]
191196
192197
--host MySQL host
193-
--port MySQL port; defaults to $mysql_port if not given
194198
--items Comma-separated list of the items whose data you want
195199
--user MySQL username; defaults to $mysql_user if not given
196200
--pass MySQL password; defaults to $mysql_pass if not given
201+
--port MySQL port; defaults to $mysql_port if not given
197202
--server-id Server id to associate with a heartbeat if heartbeat usage is enabled
198203
--nocache Do not cache results in a file
204+
--help Show usage
199205
200206
EOF;
201207
die($usage);
@@ -206,39 +212,19 @@ function usage($message) {
206212
# return them as an array ( arg => value )
207213
# ============================================================================
208214
function parse_cmdline( $args ) {
209-
$result = array();
210-
$cur_arg = '';
211-
foreach ($args as $val) {
212-
if ( strpos($val, '--') === 0 ) {
213-
if ( strpos($val, '--no') === 0 ) {
214-
# It's an option without an argument, but it's a --nosomething so
215-
# it's OK.
216-
$result[substr($val, 2)] = 1;
217-
$cur_arg = '';
215+
$options = array();
216+
while (list($tmp, $p) = each($args)) {
217+
if (strpos($p, '--') === 0) {
218+
$param = substr($p, 2);
219+
$value = null;
220+
$nextparam = current($args);
221+
if ($nextparam !== false && strpos($nextparam, '--') !==0) {
222+
list($tmp, $value) = each($args);
218223
}
219-
elseif ( $cur_arg ) { # Maybe the last --arg was an option with no arg
220-
if ( $cur_arg == '--user' || $cur_arg == '--pass' || $cur_arg == '--port' ) {
221-
# Special case because Cacti will pass these without an arg
222-
$cur_arg = '';
223-
}
224-
else {
225-
die("No arg: $cur_arg\n");
226-
}
227-
}
228-
else {
229-
$cur_arg = $val;
230-
}
231-
}
232-
else {
233-
$result[substr($cur_arg, 2)] = $val;
234-
$cur_arg = '';
224+
$options[$param] = $value;
235225
}
236226
}
237-
if ( $cur_arg && ($cur_arg != '--user' && $cur_arg != '--pass' && $cur_arg != '--port') ) {
238-
die("No arg: $cur_arg\n");
239-
}
240-
debug($result);
241-
return $result;
227+
return $options;
242228
}
243229

244230
# ============================================================================
@@ -283,7 +269,7 @@ function ss_get_mysql_stats( $options ) {
283269

284270
# First, check the cache.
285271
$fp = null;
286-
if ( !isset($options['nocache']) ) {
272+
if ( !array_key_exists('nocache', $options) ) {
287273
if ( $fp = fopen($cache_file, 'a+') ) {
288274
$locked = flock($fp, 1); # LOCK_SH
289275
if ( $locked ) {
@@ -315,14 +301,17 @@ function ss_get_mysql_stats( $options ) {
315301
}
316302
}
317303
else {
318-
debug("Couldn't lock the cache file, ignoring it.");
319304
$fp = null;
305+
debug("Couldn't lock the cache file, ignoring it");
320306
}
321307
}
308+
else {
309+
$fp = null;
310+
debug("Couldn't open the cache file");
311+
}
322312
}
323313
else {
324-
$fp = null;
325-
debug("Couldn't open the cache file");
314+
debug("Not using the cache file");
326315
}
327316

328317
# Set up variables.

docs/cacti/installing-templates.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,18 @@ PHP variables, but it is also possible to pass command-line arguments to the
185185
scripts. If you execute the script without any options, you'll see the
186186
available options. For example::
187187

188-
php ss_get_mysql_stats.php
188+
# php ss_get_mysql_stats.php
189189
Required option --host is missing
190190
Usage: php ss_get_mysql_stats.php --host <host> --items <item,...> [OPTION]
191191

192192
--host MySQL host
193-
--port MySQL port; defaults to 3306 if not given
194193
--items Comma-separated list of the items whose data you want
195194
--user MySQL username; defaults to cactiuser if not given
196195
--pass MySQL password; defaults to cactiuser if not given
196+
--port MySQL port; defaults to 3306 if not given
197197
--server-id Server id to associate with a heartbeat if heartbeat usage is enabled
198198
--nocache Do not cache results in a file
199+
--help Show usage
199200

200201
You can make Cacti pass configuration options to the script with these
201202
command-line options when it executes the script. To do this, you will need to

t/cacti/mysql_stats.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33
require('../../cacti/scripts/ss_get_mysql_stats.php');
44
$debug = true;
55

6+
is_deeply(parse_cmdline(explode(' ', '--host localhost --items jg,jh,ji,jj,jk --user --pass --port --server-id 101')),
7+
array('host' => 'localhost',
8+
'items' => 'jg,jh,ji,jj,jk',
9+
'user' => '',
10+
'pass' => '',
11+
'port' => '',
12+
'server-id' => '101'),
13+
'parse_cmdline "--host localhost --items jg,jh,ji,jj,jk --user --pass --port --server-id 101"'
14+
);
15+
16+
is_deeply(parse_cmdline(explode(' ', '--host localhost --items gg --user --pass --port 3341 --server-id ')),
17+
array('host' => 'localhost',
18+
'items' => 'gg',
19+
'user' => '',
20+
'pass' => '',
21+
'port' => '3341',
22+
'server-id' => ''),
23+
'parse_cmdline "--host localhost --items gg --user --pass --port 3341 --server-id "'
24+
);
25+
626
is(
727
make_bigint('0', '1170663853'),
828
'1170663853',

0 commit comments

Comments
 (0)