diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-06-07 08:01:46 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-06-07 08:01:46 +0300 |
commit | 757b835af8984c1fa796aec1789b484bd307bb8a (patch) | |
tree | 7f37eec457262277ff2fe856eb2907155fded780 | |
parent | ec36698ad30b99a05104952f011cf471faf83e56 (diff) | |
download | mysqlstat-757b835af8984c1fa796aec1789b484bd307bb8a.tar.gz mysqlstat-757b835af8984c1fa796aec1789b484bd307bb8a.tar.bz2 |
Keep MySQL connection open as long as possible. Implement new OIDs.
* src/MYSQL-STAT-MIB.txt: New OIDs: processTotalCount,
processActiveCount, and processSlaveCount
* src/mysqlstat.c (mysqlstat_connect): Keep connection open.
(mysqlstat_disconnect): Remove.
(get_process_list, process_first, process_next): New functions.
Keep a cached list of processes and iterate through it.
All functions updated.
(process_total_count,process_active_count)
(process_slave_count): New functions.
* src/mysqlstat.h (process_total_count,process_active_count)
(process_slave_count): New protos.
* src/mysqlstat_mib.mib2c (handle_$i): Update.
-rw-r--r-- | src/MYSQL-STAT-MIB.txt | 127 | ||||
-rw-r--r-- | src/mysqlstat.c | 220 | ||||
-rw-r--r-- | src/mysqlstat.h | 27 | ||||
-rw-r--r-- | src/mysqlstat_mib.mib2c | 114 |
4 files changed, 204 insertions, 284 deletions
diff --git a/src/MYSQL-STAT-MIB.txt b/src/MYSQL-STAT-MIB.txt index 8fbc0e6..3c05db2 100644 --- a/src/MYSQL-STAT-MIB.txt +++ b/src/MYSQL-STAT-MIB.txt | |||
@@ -19,10 +19,10 @@ IMPORTS | |||
19 | 19 | ||
20 | mysql MODULE-IDENTITY | 20 | mysql MODULE-IDENTITY |
21 | LAST-UPDATED "201606061638Z" | 21 | LAST-UPDATED "201606070800Z" |
22 | ORGANIZATION "Gray Software" | 22 | ORGANIZATION "Gray Software" |
23 | CONTACT-INFO "Sergey Poznyakoff <gray@gnu.org>" | 23 | CONTACT-INFO "Sergey Poznyakoff <gray@gnu.org>" |
24 | DESCRIPTION | 24 | DESCRIPTION |
25 | "This MIB module defines objects for MySQL statistics." | 25 | "This MIB module defines objects for MySQL statistics." |
26 | REVISION "201606061638Z" | 26 | REVISION "201606070800Z" |
27 | DESCRIPTION | 27 | DESCRIPTION |
28 | "First revision." | 28 | "First revision." |
@@ -462,100 +462,4 @@ replMasterSSLVerifyServerCert OBJECT-TYPE | |||
462 | 462 | ||
463 | -- | 463 | -- |
464 | -- Table of Replication Slave Servers | ||
465 | -- | ||
466 | |||
467 | -- replSlaveCount OBJECT-TYPE | ||
468 | -- SYNTAX Counter32 | ||
469 | -- MAX-ACCESS read-only | ||
470 | -- STATUS current | ||
471 | -- DESCRIPTION | ||
472 | -- "Number of active replication slaves." | ||
473 | -- ::= { replSlave 1 } | ||
474 | |||
475 | -- replSlaveTable OBJECT-TYPE | ||
476 | -- SYNTAX SEQUENCE OF ReplSlaveEntry | ||
477 | -- MAX-ACCESS not-accessible | ||
478 | -- STATUS current | ||
479 | -- DESCRIPTION | ||
480 | -- "A table of active replication slaves." | ||
481 | -- ::= { replSlave 2 } | ||
482 | |||
483 | -- replSlaveEntry OBJECT-TYPE | ||
484 | -- SYNTAX ReplSlaveEntry | ||
485 | -- MAX-ACCESS not-accessible | ||
486 | -- STATUS current | ||
487 | -- DESCRIPTION | ||
488 | -- "An entry (conceptual row) describing a replication | ||
489 | -- slave server." | ||
490 | -- INDEX { replSlaveIndex } | ||
491 | -- ::= { replSlaveTable 1 } | ||
492 | |||
493 | -- ReplSlaveEntry ::= SEQUENCE { | ||
494 | -- replSlaveIndex Integer32, | ||
495 | -- replSlaveHost DisplayString, | ||
496 | -- replSlaveUser DisplayString, | ||
497 | -- replSlaveCommand DisplayString, | ||
498 | -- replSlaveState DisplayString, | ||
499 | -- replSlaveTime TimeStamp, | ||
500 | -- replSlaveInfo DisplayString | ||
501 | -- } | ||
502 | |||
503 | -- replSlaveIndex OBJECT-TYPE | ||
504 | -- SYNTAX Integer32 (0..65535) | ||
505 | -- MAX-ACCESS not-accessible | ||
506 | -- STATUS current | ||
507 | -- DESCRIPTION | ||
508 | -- "A number uniquely identifying each slave server." | ||
509 | -- ::= { replSlaveEntry 1 } | ||
510 | |||
511 | -- replSlaveHost OBJECT-TYPE | ||
512 | -- SYNTAX DisplayString | ||
513 | -- MAX-ACCESS read-only | ||
514 | -- STATUS current | ||
515 | -- DESCRIPTION | ||
516 | -- "Hostname of the server." | ||
517 | -- ::= { replSlaveEntry 2 } | ||
518 | |||
519 | -- replSlaveUser OBJECT-TYPE | ||
520 | -- SYNTAX DisplayString | ||
521 | -- MAX-ACCESS read-only | ||
522 | -- STATUS current | ||
523 | -- DESCRIPTION | ||
524 | -- "User name used by that server." | ||
525 | -- ::= { replSlaveEntry 3 } | ||
526 | |||
527 | -- replSlaveCommand OBJECT-TYPE | ||
528 | -- SYNTAX DisplayString | ||
529 | -- MAX-ACCESS read-only | ||
530 | -- STATUS current | ||
531 | -- DESCRIPTION | ||
532 | -- "Command being run." | ||
533 | -- ::= { replSlaveEntry 4 } | ||
534 | |||
535 | -- replSlaveState OBJECT-TYPE | ||
536 | -- SYNTAX DisplayString | ||
537 | -- MAX-ACCESS read-only | ||
538 | -- STATUS current | ||
539 | -- DESCRIPTION | ||
540 | -- "State of the connection." | ||
541 | -- ::= { replSlaveEntry 5 } | ||
542 | |||
543 | -- replSlaveTime OBJECT-TYPE | ||
544 | -- SYNTAX TimeStamp | ||
545 | -- MAX-ACCESS read-only | ||
546 | -- STATUS current | ||
547 | -- DESCRIPTION | ||
548 | -- "Timestamp of the last status change." | ||
549 | -- ::= { replSlaveEntry 6 } | ||
550 | |||
551 | -- replSlaveInfo OBJECT-TYPE | ||
552 | -- SYNTAX DisplayString | ||
553 | -- MAX-ACCESS read-only | ||
554 | -- STATUS current | ||
555 | -- DESCRIPTION | ||
556 | -- "Additional info about the connection." | ||
557 | -- ::= { replSlaveEntry 7 } | ||
558 | |||
559 | -- | ||
560 | -- Process List | 464 | -- Process List |
561 | -- | 465 | -- |
@@ -567,5 +471,5 @@ processListTable OBJECT-TYPE | |||
567 | DESCRIPTION | 471 | DESCRIPTION |
568 | "A table of active threads." | 472 | "A table of active threads." |
569 | ::= { processList 2 } | 473 | ::= { processList 1 } |
570 | 474 | ||
571 | processListEntry OBJECT-TYPE | 475 | processListEntry OBJECT-TYPE |
@@ -661,4 +565,29 @@ processInfo OBJECT-TYPE | |||
661 | "Information about the process." | 565 | "Information about the process." |
662 | ::= { processListEntry 9 } | 566 | ::= { processListEntry 9 } |
567 | |||
568 | processTotalCount OBJECT-TYPE | ||
569 | SYNTAX Counter32 | ||
570 | MAX-ACCESS read-only | ||
571 | STATUS current | ||
572 | DESCRIPTION | ||
573 | "Total number of processes (threads)." | ||
574 | ::= { processList 2 } | ||
575 | |||
576 | processActiveCount OBJECT-TYPE | ||
577 | SYNTAX Counter32 | ||
578 | MAX-ACCESS read-only | ||
579 | STATUS current | ||
580 | DESCRIPTION | ||
581 | "Number of active processes (threads)." | ||
582 | ::= { processList 3 } | ||
583 | |||
584 | processSlaveCount OBJECT-TYPE | ||
585 | SYNTAX Counter32 | ||
586 | MAX-ACCESS read-only | ||
587 | STATUS current | ||
588 | DESCRIPTION | ||
589 | "Number of replication slaves" | ||
590 | ::= { processList 4 } | ||
591 | |||
663 | END | 592 | END |
664 | 593 | ||
diff --git a/src/mysqlstat.c b/src/mysqlstat.c index 938439a..2ffd0dd 100644 --- a/src/mysqlstat.c +++ b/src/mysqlstat.c | |||
@@ -55,32 +55,27 @@ mysqlstat_connection_t | |||
55 | mysqlstat_connect(void) | 55 | mysqlstat_connect(void) |
56 | { | 56 | { |
57 | struct mysqlstat_connection *conn; | 57 | static struct mysqlstat_connection *conn; |
58 | my_bool t = 1; | 58 | my_bool t = 1; |
59 | 59 | ||
60 | conn = xcalloc(1, sizeof(*conn)); | 60 | if (!conn) { |
61 | mysql_init(&conn->mysql); | 61 | conn = xcalloc(1, sizeof(*conn)); |
62 | if (access(config_file, F_OK) == 0) | 62 | mysql_init(&conn->mysql); |
63 | mysql_options(&conn->mysql, MYSQL_READ_DEFAULT_FILE, | 63 | if (access(config_file, F_OK) == 0) |
64 | config_file); | 64 | mysql_options(&conn->mysql, MYSQL_READ_DEFAULT_FILE, |
65 | mysql_options(&conn->mysql, MYSQL_READ_DEFAULT_GROUP, "mysqlstat"); | 65 | config_file); |
66 | mysql_options(&conn->mysql, MYSQL_OPT_RECONNECT, &t); | 66 | mysql_options(&conn->mysql, |
67 | if (!mysql_real_connect(&conn->mysql, NULL, NULL, NULL, NULL, 0, | 67 | MYSQL_READ_DEFAULT_GROUP, "mysqlstat"); |
68 | NULL, 0)) { | 68 | mysql_options(&conn->mysql, MYSQL_OPT_RECONNECT, &t); |
69 | snmp_log(LOG_ERR, "can't connect to MySQL: %s\n", | 69 | if (!mysql_real_connect(&conn->mysql, |
70 | mysql_error(&conn->mysql)); | 70 | NULL, NULL, NULL, NULL, 0, |
71 | free(conn); | 71 | NULL, 0)) { |
72 | conn = NULL; | 72 | snmp_log(LOG_ERR, "can't connect to MySQL: %s\n", |
73 | mysql_error(&conn->mysql)); | ||
74 | free(conn); | ||
75 | conn = NULL; | ||
76 | } | ||
73 | } | 77 | } |
74 | return conn; | 78 | return conn; |
75 | } | 79 | } |
76 | |||
77 | void | ||
78 | mysqlstat_disconnect(mysqlstat_connection_t conn) | ||
79 | { | ||
80 | if (!conn) | ||
81 | return; | ||
82 | mysql_close(&conn->mysql); | ||
83 | free(conn); | ||
84 | } | ||
85 | 80 | ||
86 | unsigned mysqlstat_cache_timeout = CACHE_TIMEOUT; | 81 | unsigned mysqlstat_cache_timeout = CACHE_TIMEOUT; |
@@ -102,5 +97,96 @@ unsigned mysqlstat_cache_timeout = CACHE_TIMEOUT; | |||
102 | 97 | ||
103 | #define STREQ(pat,str) (strcmp(#pat, str) == 0) | 98 | #define STREQ(pat,str) (strcmp(#pat, str) == 0) |
99 | |||
100 | struct process_list { | ||
101 | time_t ts; | ||
102 | MYSQL_RES *res; | ||
103 | uint32_t total; | ||
104 | uint32_t active; | ||
105 | uint32_t slaves; | ||
106 | uint32_t itr; | ||
107 | }; | ||
108 | |||
109 | enum { | ||
110 | FI_processID, | ||