有时候我们想能够像vmstat一样监控show slave status,当然方法有很多种,
我这里用一下自己的方法,是用C写的调用MYSQL的连接API取到数据
进行小小的处理,输出到屏幕,但是由于只是测试,没有完善,比如连接方式
应该是是参数的方式给出,我这里写死了,但是是一个方法,运行如下:
最后一个字段是字节数,忘记去掉了。
slavecheck 1 3 代表1秒一次输出3次
[root@testmy testmysqlc]# ./slavecheck 1 3
success
retrieved 1 rows
Fetched data..
Slave_IO_State**********::Waiting for master to send event::32
Master_Host**********::192.168.190.93::14
Master_User**********::repl::4
Master_Port**********::3306::4
Connect_Retry**********::60::2
Master_Log_File**********::binlog.000020::13
Read_Master_Log_Pos**********::120::3
Relay_Log_File**********::testmy-relay-bin.000005::23
Relay_Log_Pos**********::314::3
Relay_Master_Log_File**********::binlog.000020::13
Slave_IO_Running**********::Yes::3
Slave_SQL_Running**********::Yes::3
Last_Errno**********::0::1
Skip_Counter**********::0::1
Exec_Master_Log_Pos**********::120::3
Relay_Log_Space**********::682::3
Until_Condition**********::None::4
Until_Log_Pos**********::0::1
Master_SSL_Allowed**********::No::2
Seconds_Behind_Master**********::0::1
Master_SSL_Verify_Server_Cert**********::No::2
Last_IO_Errno**********::0::1
Last_SQL_Errno**********::0::1
Master_Server_Id**********::9356::4
Master_UUID**********::edc7037b-5a49-11e6-9f9d-000c29a879a3::36
Master_Info_File**********::/mysqldata/mysql5.7/data/master.info::36
SQL_Delay**********::0::1
Slave_SQL_Running_State**********::Slave has read all relay log; waiting for more updates::54
Master_Retry_Count**********::86400::5
Auto_Position**********::0::1
Retrive error:
success
retrieved 1 rows
Fetched data..
Slave_IO_State**********::Waiting for master to send event::32
Master_Host**********::192.168.190.93::14
Master_User**********::repl::4
Master_Port**********::3306::4
Connect_Retry**********::60::2
Master_Log_File**********::binlog.000020::13
Read_Master_Log_Pos**********::120::3
Relay_Log_File**********::testmy-relay-bin.000005::23
Relay_Log_Pos**********::314::3
Relay_Master_Log_File**********::binlog.000020::13
Slave_IO_Running**********::Yes::3
Slave_SQL_Running**********::Yes::3
Last_Errno**********::0::1
Skip_Counter**********::0::1
Exec_Master_Log_Pos**********::120::3
Relay_Log_Space**********::682::3
Until_Condition**********::None::4
Until_Log_Pos**********::0::1
Master_SSL_Allowed**********::No::2
Seconds_Behind_Master**********::0::1
Master_SSL_Verify_Server_Cert**********::No::2
Last_IO_Errno**********::0::1
Last_SQL_Errno**********::0::1
Master_Server_Id**********::9356::4
Master_UUID**********::edc7037b-5a49-11e6-9f9d-000c29a879a3::36
Master_Info_File**********::/mysqldata/mysql5.7/data/master.info::36
SQL_Delay**********::0::1
Slave_SQL_Running_State**********::Slave has read all relay log; waiting for more updates::54
Master_Retry_Count**********::86400::5
Auto_Position**********::0::1
Retrive error:
success
retrieved 1 rows
Fetched data..
Slave_IO_State**********::Waiting for master to send event::32
Master_Host**********::192.168.190.93::14
Master_User**********::repl::4
Master_Port**********::3306::4
Connect_Retry**********::60::2
Master_Log_File**********::binlog.000020::13
Read_Master_Log_Pos**********::120::3
Relay_Log_File**********::testmy-relay-bin.000005::23
Relay_Log_Pos**********::314::3
Relay_Master_Log_File**********::binlog.000020::13
Slave_IO_Running**********::Yes::3
Slave_SQL_Running**********::Yes::3
Last_Errno**********::0::1
Skip_Counter**********::0::1
Exec_Master_Log_Pos**********::120::3
Relay_Log_Space**********::682::3
Until_Condition**********::None::4
Until_Log_Pos**********::0::1
Master_SSL_Allowed**********::No::2
Seconds_Behind_Master**********::0::1
Master_SSL_Verify_Server_Cert**********::No::2
Last_IO_Errno**********::0::1
Last_SQL_Errno**********::0::1
Master_Server_Id**********::9356::4
Master_UUID**********::edc7037b-5a49-11e6-9f9d-000c29a879a3::36
Master_Info_File**********::/mysqldata/mysql5.7/data/master.info::36
SQL_Delay**********::0::1
Slave_SQL_Running_State**********::Slave has read all relay log; waiting for more updates::54
Master_Retry_Count**********::86400::5
Auto_Position**********::0::1
Retrive error:
给出源码
编译
gcc testcon3.c main.c -L/mysqldata/mysql5.7/lib -lmysqlclient -I/mysqldata/mysql5.7/include -g -o slavecheck
然后就可以了
如果哪天写一个监控软件可以把这个集成到子功能上去
我这里用一下自己的方法,是用C写的调用MYSQL的连接API取到数据
进行小小的处理,输出到屏幕,但是由于只是测试,没有完善,比如连接方式
应该是是参数的方式给出,我这里写死了,但是是一个方法,运行如下:
最后一个字段是字节数,忘记去掉了。
slavecheck 1 3 代表1秒一次输出3次
[root@testmy testmysqlc]# ./slavecheck 1 3
success
retrieved 1 rows
Fetched data..
Slave_IO_State**********::Waiting for master to send event::32
Master_Host**********::192.168.190.93::14
Master_User**********::repl::4
Master_Port**********::3306::4
Connect_Retry**********::60::2
Master_Log_File**********::binlog.000020::13
Read_Master_Log_Pos**********::120::3
Relay_Log_File**********::testmy-relay-bin.000005::23
Relay_Log_Pos**********::314::3
Relay_Master_Log_File**********::binlog.000020::13
Slave_IO_Running**********::Yes::3
Slave_SQL_Running**********::Yes::3
Last_Errno**********::0::1
Skip_Counter**********::0::1
Exec_Master_Log_Pos**********::120::3
Relay_Log_Space**********::682::3
Until_Condition**********::None::4
Until_Log_Pos**********::0::1
Master_SSL_Allowed**********::No::2
Seconds_Behind_Master**********::0::1
Master_SSL_Verify_Server_Cert**********::No::2
Last_IO_Errno**********::0::1
Last_SQL_Errno**********::0::1
Master_Server_Id**********::9356::4
Master_UUID**********::edc7037b-5a49-11e6-9f9d-000c29a879a3::36
Master_Info_File**********::/mysqldata/mysql5.7/data/master.info::36
SQL_Delay**********::0::1
Slave_SQL_Running_State**********::Slave has read all relay log; waiting for more updates::54
Master_Retry_Count**********::86400::5
Auto_Position**********::0::1
Retrive error:
success
retrieved 1 rows
Fetched data..
Slave_IO_State**********::Waiting for master to send event::32
Master_Host**********::192.168.190.93::14
Master_User**********::repl::4
Master_Port**********::3306::4
Connect_Retry**********::60::2
Master_Log_File**********::binlog.000020::13
Read_Master_Log_Pos**********::120::3
Relay_Log_File**********::testmy-relay-bin.000005::23
Relay_Log_Pos**********::314::3
Relay_Master_Log_File**********::binlog.000020::13
Slave_IO_Running**********::Yes::3
Slave_SQL_Running**********::Yes::3
Last_Errno**********::0::1
Skip_Counter**********::0::1
Exec_Master_Log_Pos**********::120::3
Relay_Log_Space**********::682::3
Until_Condition**********::None::4
Until_Log_Pos**********::0::1
Master_SSL_Allowed**********::No::2
Seconds_Behind_Master**********::0::1
Master_SSL_Verify_Server_Cert**********::No::2
Last_IO_Errno**********::0::1
Last_SQL_Errno**********::0::1
Master_Server_Id**********::9356::4
Master_UUID**********::edc7037b-5a49-11e6-9f9d-000c29a879a3::36
Master_Info_File**********::/mysqldata/mysql5.7/data/master.info::36
SQL_Delay**********::0::1
Slave_SQL_Running_State**********::Slave has read all relay log; waiting for more updates::54
Master_Retry_Count**********::86400::5
Auto_Position**********::0::1
Retrive error:
success
retrieved 1 rows
Fetched data..
Slave_IO_State**********::Waiting for master to send event::32
Master_Host**********::192.168.190.93::14
Master_User**********::repl::4
Master_Port**********::3306::4
Connect_Retry**********::60::2
Master_Log_File**********::binlog.000020::13
Read_Master_Log_Pos**********::120::3
Relay_Log_File**********::testmy-relay-bin.000005::23
Relay_Log_Pos**********::314::3
Relay_Master_Log_File**********::binlog.000020::13
Slave_IO_Running**********::Yes::3
Slave_SQL_Running**********::Yes::3
Last_Errno**********::0::1
Skip_Counter**********::0::1
Exec_Master_Log_Pos**********::120::3
Relay_Log_Space**********::682::3
Until_Condition**********::None::4
Until_Log_Pos**********::0::1
Master_SSL_Allowed**********::No::2
Seconds_Behind_Master**********::0::1
Master_SSL_Verify_Server_Cert**********::No::2
Last_IO_Errno**********::0::1
Last_SQL_Errno**********::0::1
Master_Server_Id**********::9356::4
Master_UUID**********::edc7037b-5a49-11e6-9f9d-000c29a879a3::36
Master_Info_File**********::/mysqldata/mysql5.7/data/master.info::36
SQL_Delay**********::0::1
Slave_SQL_Running_State**********::Slave has read all relay log; waiting for more updates::54
Master_Retry_Count**********::86400::5
Auto_Position**********::0::1
Retrive error:
给出源码
点击(此处)折叠或打开
- 文件testcon3.c
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include"mysql.h"
- void display_row(MYSQL* mycon, const MYSQL_ROW* sqlrow,MYSQL_RES* res_ptr)
- {
- unsigned int field_count;
- field_count = 0;
- MYSQL_FIELD *field_ptr;
- char* mychar;
-
- field_ptr=mysql_fetch_field(res_ptr);
- while (field_count<mysql_field_count(mycon))
- {
- //if(strlen((*sqlrow)[field_count]))
- //{
- if((*sqlrow)[field_count]&&strlen((*sqlrow)[field_count]))
- {
- printf("%30s**********::%s::%u",field_ptr->name,(*sqlrow)[field_count],strlen((char *)(*sqlrow)[field_count]));
- printf("\n");
- }
- //}
- field_count++;
- field_ptr++;
- }
- }
-
- int giveslave(void)
- {
- MYSQL *conn_ptr;
- conn_ptr = mysql_init(NULL);
- MYSQL_RES *res_ptr;
- MYSQL_ROW sqlrow;
- int res; //
- if(!conn_ptr)
- {
- printf("%s\n","error");
- return 1;
- }
-
- conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","test123","test",3307,"/mysqldata/mysql5.7/mysqld3307.sock",0);
- if(conn_ptr)
- {
- printf("%s\n","success");
- }
- else
- {
- printf("%s\n","error");
- }
-
- res = mysql_query(conn_ptr,"show slave status");
- if(res)
- {
- printf("show error:",mysql_error(conn_ptr));
- }
- else
- {
- res_ptr = mysql_store_result(conn_ptr);
- if(res_ptr)
- {
- printf("retrieved %lu rows\n",(unsigned long)mysql_num_rows(res_ptr));
- while((sqlrow = mysql_fetch_row(res_ptr)))
- {
- printf("Fetched data..\n");
- display_row(conn_ptr,&sqlrow,res_ptr);
- }
- if(mysql_error(conn_ptr))
- {
- printf("Retrive error:%s\n",mysql_error(conn_ptr));
- }
- mysql_free_result(res_ptr);
- }
- }
- mysql_close(conn_ptr);
- return 0;
- }
点击(此处)折叠或打开
- 文件main.c
- #include<stdio.h>
- #include <unistd.h>
-
-
- int main(int argc,char *argv[])
- {
- unsigned int m;
- unsigned int n;
- unsigned int i;
- if(argc != 3)
- {printf("error:");return 10;}
- sscanf(argv[2],"%u",&m);
- sscanf(argv[1],"%u",&n);
- for(i=0;i<m;i++)
- {
- giveslave();
- sleep(n);
- }
- return 0;
- }
gcc testcon3.c main.c -L/mysqldata/mysql5.7/lib -lmysqlclient -I/mysqldata/mysql5.7/include -g -o slavecheck
然后就可以了
如果哪天写一个监控软件可以把这个集成到子功能上去