iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >如何使用自己开发的MYMON工具监控MYSQL运行状态
  • 349
分享到

如何使用自己开发的MYMON工具监控MYSQL运行状态

2024-04-02 19:04:59 349人浏览 独家记忆
摘要

这篇文章主要介绍如何使用自己开发的MYMON工具监控Mysql运行状态,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、此工具展示方式:     工具将

这篇文章主要介绍如何使用自己开发的MYMON工具监控Mysql运行状态,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!




一、此工具展示方式: 
    工具将show global status中一些重要的值按照用户输入的描述和次数不断的打印到前台或者打印到
指定文件中,用于帮助DBA进行性能诊断或者瓶颈判断。
二、此工具运行原理:
   将show global status的值根据用户输入的秒数记录,前一次的值为oldvalue,新的值为newvalue,存储
在内存中,然后判断是否为累计值还是当前值,如果是累计值则newvalue-oldvalue,如果为当前值则
输出,这样则保证了输出的正确性
三、工具特点:
1、监控账号低权限只需要useage权限
2、远程连接功能
3、根据用户需要将数据写入到指定文件
4、运行日志记录,检测出错输入日志
5、5.6 5.7支持、没有测试5.5
6、核心源码开源
7、可以根据需求随时定制输出,当然需要联系本人

注意:
windows不支持,编译版本只有linux 64BIT.
并且此工具依赖mysql连接动态库文件


四、、工具使用帮助
[root@testmy 6]# ./mymon 
Author: gaopeng QQ:22389860 Mail: gaopp_200217@163.com 
Info:This tool collect data from 'show global status' and show interval (-t) seconds data change
help DBA to diagnose perfORMance problem,when error or (-d) run log is at current dir name 
is mymon.log.
WIN32 platform is not support
Useage:./mymon [-u username] [-p passWord] -S Socketfile/{-h hosname -P port} -t seconds -n times [-l logfile] [-d]
[-?]           :help info;
[-u username]  :connect to mysqld username,when no username requisite is optional;
[-p password]  :connect to mysqld password,when no password requisite is optional;
[-S socketfile]:connect to mysqld socket file;
[-h hostname]  :connect to mysqld host;
[-P port]      :connect to mysqld port;
                socketfile or [hostname port]  must;
[-t seconds]   :seconds of interval,this is necessary;
[-n times]     :how many times you watch,this is necessary;
[-l logfile]   :when you want to write information to a logfile provide a logfile name
                when a logfile provide no information at foreground,is optional;
[-d]           :more running log at logfile,otherwise error log in mymon.log;


   注意本地连接用-S连接指定socketfile即可,远程使用 -h -P连接 使用IP和端口连接 ,-l 可以直接把输出写入到指定的文件,
-d就是debug模式一般不用本工具会记录一个日志在当前目录下名字为:mymon.log,这是运行日志如果有错误会记录到日志,
开启-d 会计入更多的信息,但是一般不需要用,主要是我调试代码的时候用的.当然-t 和 -n就像帮助说的是间隔的秒数和
持续的次数,使用中如果输出有值为-1则代表值没有采集到.
   一般只需要在线上一台服务器上放上本工具,使用远程连接连接到如何服务器即可,如:
   ./mymon -h 192.168.190.91 -P3306 -t 5 -n 100 -ummon2 -pgelc123 -l /root/mysqlog/20170123mysql.log
   表示连接到服务器192.168.190.91 3306端口 5秒输出一次 输出100次,输出保留到文件/root/mysqlog/20170123mysql.log
   但是注意SysTime: Sun Jan 22 16:13:30 2017 输出是运行程序系统的当前时间,但是Uptime当然就是MYSQLD服务器的时间


四、如果获得和使用
获取:
我一共编译好了4个版本
5.7 社区版本      mymon5.7c
5.7 percona版本  mymon5.7p
5.6 社区版本       mymon5.6c
5.6 perconna版本 mymon5.6p
百度云盘如下:
Http://pan.baidu.com/s/1jIa6OfG


如果要使用更老的版本,请联系我。


使用:
下载后上传到 LINUX操作系统,
mkdir mon
cp mymon5.7p ./mon/
chmod 755 mymon5.7p
然后必须找到相应的库文件位置一般在安装位置的lib目录下,如:
[root@testmy 9]# ./mymon5.7c
./mymon5.7c: error while loading shared libraries: libmysqlclient.so.20: cannot open shared object file: No such file or directory
[root@testmy 9]# export LD_LIBRARY_PATH=/mysqldata/mysql5.7/lib
[root@testmy 9]# ./mymon5.7c
Author: gaopeng QQ:22389860 Mail: gaopp_200217@163.com 
就可以正常运行,顺便说一下5.6为 so.18 5.7为so.20跟老的为5.6以前版本


关于MYSQL监控用户权限:监控部需要什么权限,只要建立一个用户即可
mysql> create user moni@'%' identified by 'gelc123';
Query OK, 0 rows affected (0.22 sec)
mysql> show grants for  moni@'%' ;
+----------------------------------+
| Grants for moni@%                |
+----------------------------------+
| GRANT USAGE ON *.* TO 'moni'@'%' |
+----------------------------------+
1 row in set (0.06 sec)


这也最小化了顾虑,因为这个账户基本没有什么权限,如果不放心程序,可以将程序放到远程连接到线上数据库如:
./mymon5.6c -umoni -pgelc123 -h292.168.190.93 -P3307 -n 1 -t 1 -l /tmp/mysqlmon.log


这样这个程序既不会有权限而且也不在线上数据库服务器,而且可以集中管理,但是一点注意:
SysTime: Sun Jan 22 16:13:30 2017 输出是运行程序系统的当前时间,但是Uptime当然就是MYSQLD服务器的时间




五、此工具展示内容:
展示的内容并不是固定,后期根据需求可以进行简单的加入指标即可,当前输出为如下:
------------------------------------------MYMON--------------------------------------------
SysTime: Sun Jan 22 16:13:30 2017
Uptime:6963226    Connections:1       Aborted_clients:0       Max_used_connections:1319    
--------------------------------------MYSQLINFO-------------------------------------------
[###Threads info and qc  and :]
Threads_connected:252         Threads_running:4             Qcache_free_memory:0            
Qcache_hits:0                 Qcache_not_cached:0           Qcache_inserts:0                
[###Table read info:]
Handler_read_first:211        Handler_read_key:8804         Handler_read_next:42346         
Handler_read_last:0           Handler_read_rnd:73           Handler_read_rnd_next:717694
[###Sorts and temp tables info:]
Sort_rows:0                   Sort_merge_passes:0
Created_tmp_tables:163        Created_tmp_disk_tables:152
[###Table join info:]
Select_full_join:1            Select_scan:207
[###Open table cache and binlog cache info:]
Table_open_cache_hits:4938    Table_open_cache_misses:0
Binlog_cache_use:820          Binlog_cache_disk_use:0
[###Hanler_xxx and Com_xxx info:]
Handler_delete:34574          Handler_update:675            Handler_write:1364
Handler_commit:5833           Handler_rollback:10
Com_commit:156                Com_rollback:5
U(counts):631     D(counts):159     I(counts):47      S(counts):4955    
--------------------------------------INNODBINFO------------------------------------------
[###Innodb_lock info:]
Innodb_row_lock_time:0                Innodb_row_lock_waits:0
Innodb_row_lock_current_waits:38
[###Innodb_buffer_pool info:]
Innodb_buf_pool_pages_total:1965840   Innodb_buf_pool_pages_dirty:19154
Innodb_buf_pages_data:1940433         Innodb_buf_pages_free:8116
Innodb_buffer_pool_wait_free:0        Innodb_buffer_pool_pages_misc:17291
Innodb_dblwr_pages_written:1188       Innodb_dblwr_writes:80
Innodb_buffer_pool_pages_flushed:2249
read_ahead:0          ahead_evict:0          ahead_rnd:0   
[###Innodb_log info:]
Innodb_log_writes:796                 Innodb_log_waits:0
Innodb_os_log_pending_fsyncs:0        Innodb_os_log_pending_writes:0
Innodb_os_log_written:44073472
[###Innodb_data read/writes info:]
Innodb_data_reads:684                 Innodb_data_read:11206656
Innodb_data_writes:2067               Innodb_data_written:63603200
Innodb_data_fsyncs:1017               Innodb_data_pending_fsyncs:0
Innodb_buffer_pool_reads(physics reads):423
Innodb_buffer_pool_read_requests(logic reads):2869823
[###Innodb_data rows info:]
U(rows):675     D(rows):34575   I(rows):194     S(rows):767094  
---------------------------------------TOTALINFO------------------------------------------
Bytes_received:911280         Bytes_sent:8653956


六、监控值说明:
(本部分,带有作者自己的理解,如果有误请指出)


文中
当前值输出:保持show global status的当前值输出
累计值相减输出:用上一次的值减去下一次的值输出,间隔为用户指定的秒数


第一部分:
SysTime: Sun Jan 22 16:13:30 2017
Uptime:6963226    Connections:1       Aborted_clients:0       Max_used_connections:1319    


Uptime:The number of seconds the MySQL Server has been running.(当前值输出 单位秒)
Connections:The number of connection attempts (successful or not) to the MySQL server.(累计值相减输出 单位次数)
Aborted_clients:The number of connections that were aborted because the client died without closing the connection
                properly.(累计值相减输出,单位连接数)
Max_used_connections:The maximum number of connections that have been in use simultaneously since the server started.
                     (当前值输出,单位连接数)
第二部分:
[###Threads info and qc  and :]
Threads_connected:252         Threads_running:4             Qcache_free_memory:0            
Qcache_hits:0                 Qcache_not_cached:0           Qcache_inserts:0        


Threads_connected:The number of currently open connections.(当前值输出 单位连接数)
Threads_running:The number of threads that are not sleeping.(当前值输出 单位连接数)
Qcache_free_memory:The amount of free memory for the query cache.(当前值输出 单位字节数)
Qcache_hits:The number of query cache hits.(累计值相减输出 单位次数)
Qcache_not_cached:The number of noncached queries.(累计值相减输出 单位次数)
Qcache_inserts:The number of queries added to the query cache.(累计值相减输出 单位次数)


第三部分:
本部分全部是(累计值相减输出 单位次数)
[###Table read info:]
Handler_read_first:211        Handler_read_key:8804         Handler_read_next:42346         
Handler_read_last:0           Handler_read_rnd:73           Handler_read_rnd_next:717694


Handler_read_first:
         
Handler_read_key:
         
Handler_read_next:
         
Handler_read_last:
       


Handler_read_rnd,Handler_read_rnd_next:这两个值较大则可能遇到了全表扫描及type=ALL


这一部分则可以大体掌握当前数据库全表扫描和索引使用的大概情况  
第四部分:
[###Sorts and temp tables info:]
Sort_rows:0                   Sort_merge_passes:0
Created_tmp_tables:163        Created_tmp_disk_tables:152


Sort_rows:The number of sorted rows.(累计值相减输出 单位行数)
Sort_merge_passes:The number of merge passes that the sort alGorithm has had to do.(累计值相减输出 单位次数)
Created_tmp_tables:The number of internal temporary tables created by the server while executing statements.(累计值相减输出 单位次数)
Created_tmp_disk_tables:The number of internal on-disk temporary tables created by the server while executing statements.(累计值相减输出 单位次数)


很明显这一部分说明了排序和临时表使用情况,可以帮助DBA进行判断


第五部分:
[###Table join info:]
Select_full_join:1            Select_scan:207

Select_full_join:The number of joins that perform table scans because they do not use indexes. 
                 If this value is not 0, you should carefully check the indexes of your tables
                 (累计值相减输出 单位次数)
Select_scan:The number of joins that did a full scan of the first table. (累计值相减输出 单位次数)

这两个值则说明了jion的时候是否使用到了索引,特别是Select_full_join,应该比较低,否则说明被驱动表没有使用到索引
应该引起重视,优化语句,一般来讲Select_scan比较高是正常的因为驱动表如果没有谓词条件使用全表扫描是正常的方式


第六部分:
[###Open table cache and binlog cache info:]
Table_open_cache_hits:4938    Table_open_cache_misses:0
Binlog_cache_use:820          Binlog_cache_disk_use:0


Table_open_cache_hits:The number of hits for open tables cache lookups.(累计值相减输出 单位次数)
Table_open_cache_misses:The number of misses for open tables cache lookups. (累计值相减输出 单位次数)
Binlog_cache_use: The number of transactions that used the binary log cache. (累计值相减输出 单位事物数)
Binlog_cache_disk_use:The number of transactions that used the temporary binary log cache but that exceeded the value 
                                 of binlog_cache_size and used a temporary file to store statements from the transaction.(累计值相减输出 单位事物数)
明显这部分说明了binlog_cache_size和 table_open_cache是否合适


第七部分:
本部分全部是(累计值相减输出 单位次数)
[###Hanler_xxx and Com_xxx info:]
Handler_delete:34574          Handler_update:675            Handler_write:1364
Handler_commit:5833           Handler_rollback:10
Com_commit:156                Com_rollback:5
U(counts):631     D(counts):159     I(counts):47      S(counts):4955    

U(counts):631     D(counts):159     I(counts):47      S(counts):4955  
这一行是Com_update\Com_delete\Com_insert\Com_select的简写

Handler_xxx:这一批值说明的是相应操作处理的次数
Com_xxx:这一批值说明是相应操作对应语句发出的次数

比如Handler_commit 和 Com_commit这不同,如果是隐含提交和自动提交不用发起commit,但是
commit是处理了的,那么Handler_commit增加而Com_rollback不变,又比如手动执行一个commit
Com_commit增加Handler_commit不变,因为虽然发起了commit命令并不需要处理实验如下:


mysql> show status like '%commit%';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| Com_commit     | 1     |
| Com_xa_commit  | 0     |
| Handler_commit | 4     |
+----------------+-------+
3 rows in set (0.01 sec)


mysql> commit;
Query OK, 0 rows affected (0.00 sec)


mysql> show status like '%commit%';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| Com_commit     | 2     |
| Com_xa_commit  | 0     |
| Handler_commit | 4     |
+----------------+-------+
3 rows in set (0.00 sec)


第八部分:
[###Innodb_lock info:]
Innodb_row_lock_time:0                Innodb_row_lock_waits:0
Innodb_row_lock_current_waits:38


Innodb_row_lock_time:The total time spent in acquiring row locks for InnoDBtables, in milliseconds.(累计值相减输出 单位毫秒)
Innodb_row_lock_waits:The number of times operations on InnoDB tables had to wait for a row lock.(累计值相减输出 单位次数)
Innodb_row_lock_current_waits:The number of row lockscurrently being waited for by operations on InnoDB tables.(当前值输出 单位个数)


明显这一部分是对innodb 的状态的监控


第九部分:


[###Innodb_buffer_pool info:]
Innodb_buf_pool_pages_total:1965840   Innodb_buf_pool_pages_dirty:19154
Innodb_buf_pages_data:1940433         Innodb_buf_pages_free:8116
Innodb_buffer_pool_wait_free:0        Innodb_buffer_pool_pages_misc:17291
Innodb_dblwr_pages_written:1188       Innodb_dblwr_writes:80
Innodb_buffer_pool_pages_flushed:2249
read_ahead:0          ahead_evict:0          ahead_rnd:0       


Innodb_buf_pool_pages_total:及Innodb_buffer_pool_pages_data The total size of the InnoDB buffer pool, in pages.(当前值输出 单位页)
Innodb_buf_pool_pages_dirty:及Innodb_buffer_pool_pages_dirty The current number of dirty pagesin the InnoDB buffer pool.(当前值输出 单位页)
Innodb_buf_pages_data:及Innodb_buffer_pool_pages_data The number of pagesin the InnoDB buffer poolcontaining data. The number includes both dirtyand
                      clean pages.(当前值输出 单位页)
Innodb_buf_pages_free:及Innodb_buffer_pool_pages_free The number of free pagesin the InnoDB buffer pool.(当前值输出 单位页)
Innodb_buffer_pool_wait_free:Normally, writes to the InnoDB buffer poolhappen in the background. When InnoDBneeds to read or
                             create a pageand no clean pages are available, InnoDBflushes some dirty pagesfirst and waits for that
                             operation to finish. This counter counts instances of these waits.(累计值相减输出 单位次数)


Innodb_buffer_pool_pages_misc:The number of pages in the InnoDB buffer pool that are busy because they have
                              been allocated for administrative overhead, such as row locks or the adaptive hash
                              index. This value can also be calculated as Innodb_buffer_pool_pages_total ?
                              Innodb_buffer_pool_pages_free ? Innodb_buffer_pool_pages_data.(当前值输出 单位页)


Innodb_dblwr_pages_written:The number of pages that have been written to the doublewrite buffer.(累计值相减输出 单位页)
Innodb_dblwr_writes:The number of doublewrite operations that have been performed. (累计值相减输出 单位次数)
Innodb_buffer_pool_pages_flushed:The number of requests to flush pages from the InnoDB buffer pool.(累计值相减输出 单位次数)


这部分是innodb_buffer_pool的重点包含了脏页,数据页,空闲页等信息,以及刷磁盘的次数


第十部分:
[###Innodb_log info:]
Innodb_log_writes:796                 Innodb_log_waits:0
Innodb_os_log_pending_fsyncs:0        Innodb_os_log_pending_writes:0
Innodb_os_log_written:44073472




Innodb_log_writes:The number of physical writes to the InnoDB redo logfile.(累计值相减输出 单位次数)
Innodb_log_waits:The number of times that the log buffer was too small and a wait was required for it to be flushed before
                               continuing.(累计值相减输出 单位次数)
Innodb_os_log_pending_fsyncs:The number of pending fsync() operations for the InnoDB redo logfiles.(累计值相减输出 单位次数)
Innodb_os_log_pending_writes:The number of pending writes to the InnoDB redo logfiles.(累计值相减输出 单位次数)
Innodb_os_log_written:The number of bytes written to the InnoDB redo logfiles.(累计值相减输出 单位字节)


这部分说明了 innodb redo 写的信息,如写了多少数据,多少次等待,如果Innodb_os_log_pending_writes和Innodb_os_log_pending_fsyncs
不为0则说明遭遇了I/O等待。而Innodb_log_waits不为0则考虑innodb_log_buffer_size 是否过小。
注意:不管innodb_flush_method设置如何 redo始终使用kernel buffer。


第十一部分:
[###Innodb_data read/writes info:]
Innodb_data_reads:684                 Innodb_data_read:11206656
Innodb_data_writes:2067               Innodb_data_written:63603200
Innodb_data_fsyncs:1017               Innodb_data_pending_fsyncs:0
Innodb_buffer_pool_reads(physics reads):423
Innodb_buffer_pool_read_requests(logic reads):2869823


Innodb_data_reads:The total number of data reads.(累计值相减输出 单位次数)
Innodb_data_read:The amount of data read since the server was started(累计值相减输出 单位字节数)
Innodb_data_writes:The total number of data writes.(累计值相减输出 单位次数)
Innodb_data_written:The amount of data written so far, in bytes.(累计值相减输出 单位字节数)
Innodb_data_fsyncs:The number of fsync()operations so far. The frequency of fsync()calls is influenced by the setting
                            of the innodb_flush_method configuration option.(累计值相减输出 单位次数)
Innodb_data_pending_fsyncs:The current number of pending fsync()operations. The frequency of fsync()calls is influenced by
                           the setting of the innodb_flush_method configuration option.(累计值相减输出 单位次数)
Innodb_buffer_pool_reads(physics reads):The number of logical reads that InnoDB could not satisfy from the buffer pool, and had to read 
                                                        directly from disk.(累计值相减输出 单位次数)
Innodb_buffer_pool_read_requests(logic reads):The number of logical read requests.(累计值相减输出 单位次数)
read_ahead:及Innodb_buffer_pool_read_ahead:The number of pages read into the InnoDB buffer pool by the read-ahead background thread.(累计值相减输出 单位页)
ahead_evict:及Innodb_buffer_pool_read_ahead_evicted:The number of pagesread into the InnoDB buffer poolby the read-aheadbackground thread that were
                                                                                subsequently evictedwithout having been accessed by queries.(累计值相减输出 单位页)
ahead_rnd:及Innodb_buffer_pool_read_ahead_rnd:The number of “random”read-aheads initiated by InnoDB. This happens when a query scans a large
                                                                        portion of a table but in random order.(累计值相减输出 单位次数)


这一部分可以看出逻辑读取的命中率,预读效率等,注意Innodb_data_pending_fsyncs和Innodb_data_fsyncs如果开启了O_DIRECT,那么innodb_buffer的内存
数据不会出现在kernel buffer,那么fsyncs pending的情况会少得多。如果这两个值较大注意磁盘I/O


第十二部分:
本部分全部是(累计值相减输出 单位行数)
U(rows):675     D(rows):34575   I(rows):194     S(rows):767094  


U:Innodb_rows_updated
D:Innodb_rows_deleted
I:Innodb_rows_inserted
S:Innodb_rows_read


这部分真正的反应了DML和SELECT的行数,注意和Com_xxx和Handler_xxx的区别


第十三部分:
Bytes_received:911280         Bytes_sent:8653956


没什么好说的自己查文档


七、内存溢出检测
    使用:
valgrind --tool=memcheck --leak-check=full  ./mymon5.7c -S/mysqldata/mysql5.7sla/mysqld3307.sock -t 1 -n 10
    
==5120== LEAK SUMMARY:
==5120==      definitely lost: 0 bytes in 0 blocks
==5120==      indirectly lost: 0 bytes in 0 blocks
==5120==      possibly lost: 232 bytes in 2 blocks
==5120==      still reachable: 8,176 bytes in 2 blocks
==5120==      suppressed: 0 bytes in 0 blocks
==5120== Reachable blocks (those to which a pointer was found) are not shown.
==5120== To see them, rerun with: --leak-check=full --show-reachable=yes
==5120== 
==5120== For counts of detected and suppressed errors, rerun with: -v
==5120== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)
可以看到没有内存溢出


八、核心源码


点击(此处)折叠或打开

  1. main主调函数:



  2. #include"type.h"


  3. int useage(void)

  4. {

  5.         printf(

  6.             "Author: gaopeng QQ:22389860 Mail: gaopp_200217@163.com \n"

  7.                         "Info:This tool collect data from 'show global status' and show interval (-t) seconds data change\n"

  8.                         "help DBA to diagnose performance problem,when error or (-d) run log is at current dir name \n"

  9.                         "is mymon.log.\n"

  10.                         "WIN32 platform is not support\n"

  11.                         "Useage:./mymon [-u username] [-p password] -S socketfile/{-h hosname -P port} -t seconds -n times [-l logfile] [-d]\n"

  12.                         "[-?]           :help info;\n"

  13.                         "[-u username]  :connect to mysqld username,when no username requisite is optional;\n"

  14.                         "[-p password]  :connect to mysqld password,when no password requisite is optional;\n"

  15.                         "[-S socketfile]:connect to mysqld socket file;\n"

  16.                         "[-h hostname]  :connect to mysqld host;\n"

  17.                         "[-P port]      :connect to mysqld port;\n"

  18.                         "                socketfile or [hostname port]  must;\n"

  19.                         "[-t seconds]   :seconds of interval,this is necessary;\n"

  20.                         "[-n times]     :how many times you watch,this is necessary;\n"

  21.                         "[-l logfile]   :when you want to write information to a logfile provide a logfile name\n"

  22.                         "                when a logfile provide no information at foreground,is optional;\n"

  23.                         "[-d]           :more running log at logfile,otherwise error log in mymon.log;\n"

  24.                 );

  25.         return 0;

  26. }



  27. int wel(void)

  28. {

  29.         printf(

  30.             "welcome used this mysql monitor tool\n"

  31.                   "Author: gaopeng QQ:22389860 Mail: gaopp_200217@163.com \n"

  32.                         "Info:This tool collect data from 'show global status' and show interval (-t) seconds data change\n"

  33.                         "help DBA to diagnose performance problem,when error or (-d) run log is at current dir name \n"

  34.                         "is mymon.log.\n"

  35.                         "WIN32 platform is not support\n"

  36.                         "Useage:./mymon [-u username] [-p password] -S socketfile/{-h hosname -P port} -t seconds -n times [-l logfile] [-d]\n"

  37.                         "more help info in ./mymon -h\n"

  38.                         );

  39.         return 0;

  40. }


  41. int getpar( char (*par)[500],int n)

  42. {

  43.         int i;

  44.         for(i=0;i<4;i++)

  45.         {

  46.                 printf("your parameter is %s\n",*(par+i));

  47.         }

  48. }


  49. int checkpar(char (*par)[500],int t,int n)

  50. {

  51.         if(strlen(*(par+2)) == 0 && (strlen(*(par+4)) == 0 || strlen(*(par+5)) == 0 ))

  52.         {


  53.                 useage();

  54.                 return -1;

  55.         }


  56.         if(t==0||n==0)

  57.         {

  58.                 useage();

  59.                 return -1;

  60.         }

  61.         return 0;

  62. }


  63. int main(int arGC,char* argv[])

  64. {

  65.         int ch;

  66.         int t = 0; //t how long

  67.         int n = 0;//n count

  68.         FILE* fd = 0;//log file fs

  69.         unsigned int mport = 0;//mysql connect port


  70.         debug = 0;

  71.         write_file = 0;

  72.         PAR par;

  73.         setbuf(stdout,NULL);//stdio buffer disable

  74.         memset(par,0,sizeof(PAR));

  75.         if(argc<2)

  76.         {

  77.                 useage();

  78.                 return -1;

  79.         }

  80.         while ((ch = getopt(argc, argv, "u:p:S:t:n:l:h:P:d?")) != -1)

  81.         {

  82.                 switch (ch)

  83.                 {

  84.                         case 'u':

  85.                                 if(strlen(optarg)>MAX_PAR_LEN)

  86.                                 {

  87.                                         printf("MYMON:username max length more than 500 bytes\n");

  88.                                         return -1;

  89.                                 }

  90.                                 strcpy(par[0],optarg);

  91.                                 break;

  92.                         case 'h':

  93.                                 if(strlen(optarg)>MAX_PAR_LEN)

  94.                                 {

  95.                                         printf("MYMON:hostname max length more than 500 bytes\n");

  96.                                         return -1;

  97.                                 }

  98.                                 strcpy(par[4],optarg);

  99.                                 break;

  100.                         case 'P':

  101.                                 if(strlen(optarg)>MAX_PAR_LEN)

  102.                                 {

  103.                                         printf("MYMON:port max length more than 500 bytes\n");

  104.                                         return -1;

  105.                                 }

  106.                                 strcpy(par[5],optarg);

  107.                                 sscanf (par[5],"%u",&mport);

  108.                                 break;

  109.                         case 'p':

  110.                                 if(strlen(optarg)>MAX_PAR_LEN)

  111.                                 {

  112.                                         printf("MYMON:password max length more than 500 bytes\n");

  113.                                         return -1;

  114.                                 }

  115.                                 strcpy(par[1],optarg);

  116.                                 break;

  117.                         case 'S':

  118.                                 if(strlen(optarg)>MAX_PAR_LEN)

  119.                                 {

  120.                                         printf("MYMON:socket max length more than 500 bytes\n");

  121.                                         return -1;

  122.                                 }

  123.                                 strcpy(par[2],optarg);

  124.                                 break;

  125.                         case 't':

  126.                                 sscanf (optarg,"%d",&t);

  127.                                 break;

  128.                         case 'n':

  129.                                 sscanf (optarg,"%d",&n);

  130.                                 break;

  131.                         case 'l':

  132.                                 if(strlen(optarg)>MAX_PAR_LEN)

  133.                                 {

  134.                                         printf("MYMON:logfile name max length more than 500 bytes\n");

  135.                                         return -1;

  136.                                 }

  137.                                 strcpy(par[3],optarg);

  138.                                 if((fd = fopen(par[3],OPENFLAG)) == NULL)

  139.                                 {

  140.                                         perror("MYMON:");

  141.                                         return -1;

  142.                                 }

  143.                                 write_file = 1;

  144.                                 setbuf(fd,NULL);//stdio buffer disable

  145.                                 break;

  146.                         case 'd':

  147.                                 debug = 1;

  148.                                 break;

  149.                         default:

  150.                         case '?':

  151.                                 useage();

  152.                                 return -1;

  153.                 }

  154.         }


  155.         if(checkpar(par,t,n) == -1)

  156.         {

  157.                 printf("MYMON:must parameter error\n");

  158.                 return -1;

  159.         }

  160.         wel();

  161.         if(conn(par,t,n+1,fd,mport) !=0)

  162.                 {

  163.                         printf("MYMON:error pelase check log\n");

  164.                         return -1;

  165.                 }

  166.         if(write_file == 1)

  167.                 {

  168.                         fclose(fd);

  169.                 }

  170.         return 0;

  171. }

点击(此处)折叠或打开

  1. fun.c功能实现接口函数


  2.  


  3.  


  4. #include"type.h"

  5. #include"fun.h"





  6. typedef struct MDATA

  7. {

  8.         char* statname;

  9.         char* olddata;

  10.         char* newdata;

  11. } MDATA;


  12. typedef struct D_STROE

  13. {

  14.         char statname[MAXSTATNAME];

  15.         int64_t data;

  16. } D_STROE;




  17. int handle_data(MYSQL_RES *result,uint64_t row_count,unsigned int row_field,FILE* fd,MDATA* *datap,int i);


  18. int memaloc(MDATA** *datap,uint64_t row_count);


  19. int sub_store_data(MDATA* *datap,D_STROE* *last_data,uint64_t row_count,int* lenth);


  20. int print_data(D_STROE* last_data,int lenth,FILE* fd) ;


  21. int64_t findva(const char* ins,D_STROE* last_data,int lenth);


  22. int freedatap(MDATA* *datap,uint64_t row_count);


  23. static freecouti=0;


  24. void test(void)

  25. {

  26.         ;

  27. }



  28. int mprintf(FILE* outfile,const char *format,...)

  29. {

  30.     va_list args;

  31.     va_start(args,format);

  32.     if( vfprintf(outfile,format,args) <0 )

  33.         {

  34.                 GET_LOG(LogLevel[4],ERROR,"ERROR:mprintf() error:%d\n",ERROR);

  35.                         printf("MYMON: mprintf() error:%d\n",ERROR);

  36.                         exit(-1);

  37.         }

  38.     va_end(args);

  39.         return 0;

  40. }




  41. int xfree(void* p)

  42. {

  43.         if(p==NULL)

  44.         {

  45.                 freecouti++;

  46.                 if(debug == 3)

  47.                 {

  48.                         GET_LOG(LogLevel[1],DEBUG,"DEBUG:xfree(NULL) debug:%d\n",freecouti);

  49.                 }

  50.                 return 0;

  51.         }

  52.         free(p);

  53.         p=NULL;

  54.         return 0;

  55. }


  56. int conn(char (*par)[500],int t,int n,FILE* fd,unsigned int myport)

  57. {

  58.         MYSQL conn_ptr;

  59.         MYSQL_RES *query_res =NULL;

  60.         MYSQL_ROW sqlrow_data;

  61.         int ret = 0;

  62.         uint64_t  row_count = 0;

  63.         unsigned int row_field = 0;

  64.         MDATA* *datap =NULL;

  65.         int i=0;//used to check frist time no old data



  66.         

  67.         if(mysql_init(&conn_ptr) == NULL)

  68.         {

  69.                 ret = -1;

  70.                 GET_LOG(LogLevel[4],ERROR,"ERROR:mysql_init() error:%d\n",ERROR);

  71.                 printf("MYMON: mysql_init() error\n");

  72.                 return ret;

  73.         }

  74.         

  75.         if(mysql_real_connect(&conn_ptr,*(par+4),*par,*(par+1),NULL,myport,*(par+2),0)==NULL)

  76.         {

  77.                 ret = -2;

  78.                 GET_LOG(LogLevel[4],ERROR,"ERROR:mysql_real_connect() error:%d\n",ERROR);

  79.                 printf("MYMON: mysql_real_connect() error\n");

  80.                 if(mysql_errno(&conn_ptr))

  81.                 {

  82.                         GET_LOG(LogLevel[4],ERROR,"ERROR:mysql_real_connect() error:%s\n",mysql_error(&conn_ptr));

  83.                         printf("MYMON: mysql_real_connect() error:%s\n",mysql_error(&conn_ptr));

  84.                 }

  85.                 return ret;

  86.         }


  87.         while(n--)

  88.         {

  89.                 

  90.                 if((mysql_query(&conn_ptr,"show global status")) != 0)

  91.                 {

  92.                         ret = -3;

  93.                         GET_LOG(LogLevel[4],ERROR,"ERROR:mysql_query() error:%s\n",mysql_error(&conn_ptr));

  94.                         printf("MYMON: mysql_query() error:%s\n",mysql_error(&conn_ptr));

  95.                         return ret;

  96.                 }

  97.                 


  98.                 if((query_res=mysql_store_result(&conn_ptr)) == NULL)

  99.                 {

  100.                         ret = -4;

  101.                         GET_LOG(LogLevel[4],ERROR,"ERROR:mysql_store_result() error:%s\n",mysql_error(&conn_ptr));

  102.                         printf("MYMON: mysql_store_result() error:%s\n",mysql_error(&conn_ptr));

  103.                         return ret;

  104.                 }

  105.                 

  106.                 row_count = mysql_num_rows(query_res);

  107.                 row_field = mysql_field_count(&conn_ptr);

  108.                 if(debug == 1)

  109.                 {

  110.                         GET_LOG(LogLevel[2],INFO,"INFO:mysql_num_rows() rows is %llu\n",row_count);

  111.                         GET_LOG(LogLevel[2],INFO,"INFO:mysql_field_count() fileds is %ld\n",row_field);

  112.                         printf("MYMON: mysql_num_rows() rows is %llu\n",row_count);

  113.                         printf("MYMON: mysql_field_count() fileds is %ld\n",row_field);

  114.                 }

  115.                 if(i == 0)

  116.                 {

  117.                         if((memaloc(&datap,row_count)) !=0)

  118.                         {

  119.                                 ret = -6;

  120.                                 GET_LOG(LogLevel[4],ERROR,"ERROR:memaloc() error:%s\n","ERROR");

  121.                                 printf("MYMON: memaloc() error:%s\n","ERROR");

  122.                                 return ret;

  123.                         }

  124.                 }

  125.                 if(handle_data(query_res,row_count,row_field,fd,datap,i)!=0)

  126.                 {

  127.                         ret = -5;

  128.                         GET_LOG(LogLevel[4],ERROR,"ERROR:handle_data() error:%s\n","ERROR");

  129.                         printf("MYMON: handle_data() error:%s\n","ERROR");

  130.                         return ret;

  131.                 }


  132.                 

  133.                 mysql_free_result(query_res);

  134.                 sleep(t);

  135.                 i++;

  136.         }

  137.         


  138.         

  139.                 {

  140.                         freedatap(datap,row_count);

  141.                 }

  142.         mysql_close(&conn_ptr);

  143.         return ret;

  144. }



  145. int freedatap(MDATA* *datap,uint64_t row_count)

  146. {

  147.         int ret = 0;

  148.         uint64_t i = 0;

  149.         if(datap == NULL)

  150.                 {

  151.                         return ret;

  152.                 }

  153.         GET_LOG(LogLevel[3],INFO,"INFO:freedatap()  check previous line is error?:%d\n",ret);

  154.         for(i=0;i<row_count;i++)

  155.                 {

  156.                         xfree ((*(datap+i))->statname);

  157.                         xfree((*(datap+i))->newdata);

  158.                         xfree((*(datap+i))->olddata);

  159.                         xfree(*(datap+i));

  160.                 }

  161.         xfree(datap);

  162.         return ret;

  163. }

  164. int memaloc(MDATA** *datap,uint64_t row_count)

  165. {

  166.         int ret=0;

  167.         MDATA* *mp=NULL;

  168.         uint64_t i=0;


  169.         if(datap == NULL)

  170.         {

  171.                 ret = -5;

  172.                 GET_LOG(LogLevel[4],ERROR,"ERROR:memaloc()  datap=NULL?:%d\n",ret);

  173.                 printf("MYMON: memaloc()  datap=NULL?:%d\n",ret);

  174.                 return ret;

  175.         }

  176.         if((mp = (MDATA**)calloc(1,row_count*sizeof(MDATA*))) == NULL)

  177.         {

  178.                 ret = -3;

  179.                 GET_LOG(LogLevel[4],ERROR,"ERROR:memaloc() memory malloc **p error:%d\n",ret);

  180.                 printf("MYMON: memaloc() memory malloc **p error:%d\n",ret);

  181.                 return ret;

  182.         }

  183.         for(i=0;i<row_count;i++)

  184.         {

  185.                 if((*(mp+i)=(MDATA*)calloc(1,sizeof(MDATA))) == NULL)

  186.                 {

  187.                         ret=-4;

  188.                         GET_LOG(LogLevel[4],ERROR,"ERROR:handle_data() memory malloc *p error:%d\n",ret);

  189.                         printf("MYMON: MYMON:handle_data() memory malloc *p error:%d\n",ret);

  190.                         freedatap(mp,row_count);

  191.                         return ret;

  192.                 }

  193.         }

  194.         *datap = mp;

  195.         return ret;


  196. }



  197. int handle_data(MYSQL_RES *result,uint64_t row_count,unsigned int row_field,FILE* fd,MDATA* *datap,int seq)

  198. {

  199.         uint64_t i = 0;

  200.         MYSQL_ROW sqlrow_data;

  201.         int ret = 0;

  202.         D_STROE* last_data=NULL;//used last data store

  203.         int lenth=0;//last data lenth


  204.         //tmp

  205.         int tmp = 0;

  206.     D_STROE* tmp_last_data = NULL;


  207.         if(result == NULL||datap == NULL)

  208.         {

  209.                 ret = -2;

  210.                 GET_LOG(LogLevel[4],ERROR,"ERROR:handle_data() result == NULL datap == NULL :%s\n","ERROR");

  211.                 printf("MYMON:handle_data() result == NULL datap == NULL :%s\n","ERROR");

  212.                 return ret;

  213.         }


  214.         for(i=0;i<row_count;i++)//control true result return

  215.         {

  216.                 

  217.                 if((sqlrow_data=mysql_fetch_row(result)) == NULL) //control mysql_fetch_row error

  218.                 {

  219.                         ret = -1;

  220.                         GET_LOG(LogLevel[4],ERROR,"ERROR:mysql_fetch_row() error:%s\n","ERROR");

  221.                         printf("MYMON: mysql_fetch_row() error:%s\n","ERROR");

  222.                         return ret;

  223.                 }

  224.                 {

  225.                         unsigned long *lengths;

  226.                         lengths = mysql_fetch_lengths(result);

  227.                         xfree((*(datap+i))->statname);

  228.                         if(((*(datap+i))->statname = (char*)calloc(1,lengths[0]+10)) == NULL)

  229.                         {

  230.                                 ret = -4;

  231.                                 GET_LOG(LogLevel[4],ERROR,"ERROR:handle_data() statname loop mem alloc error:%s\n","ERROR");

  232.                                 printf("MYMON: handle_data() statname loop mem alloc error:%s\n","ERROR");

  233.                                 return ret;


  234.                         }

  235.                         strcpy((*(datap+i))->statname,sqlrow_data[0] ? sqlrow_data[0] : "NULL");

  236.                         xfree((*(datap+i))->olddata);

  237.                         (*(datap+i))->olddata = (*(datap+i))->newdata;

  238.                         //no free newdata pointer

  239.                         if(((*(datap+i))->newdata = (char*)calloc(1,lengths[1]+10)) == NULL)

  240.                         {

  241.                                 ret = -5;

  242.                                 GET_LOG(LogLevel[4],ERROR,"ERROR:handle_data() newdata loop mem alloc error:%s\n","ERROR");

  243.                                 printf("MYMON: handle_data() newdata loop mem alloc error:%s\n","ERROR");

  244.                                 return ret;

  245.                         }

  246.                         strcpy((*(datap+i))->newdata,sqlrow_data[1] ? sqlrow_data[1] : "NULL");

  247.                         if(debug == 1)

  248.                                 {

  249.                                if((!strcmp((*(datap+i))->statname,"NULL")) || (!strcmp((*(datap+i))->newdata,"NULL")) )

  250.                                 {

  251.                                         GET_LOG(LogLevel[1],DEBUG,"DEBUG:handle_data() statname or data IS NULL:[%s]\n",(*(datap+i))->statname);

  252.                                                 GET_LOG(LogLevel[1],DEBUG,"DEBUG:handle_data() statname or data IS NULL:[%s]\n",(*(datap+i))->newdata);

  253.                                 }

  254.                                 }

  255.                 }


  256.         }

  257.         if(seq == 0)//olddata have no data

  258.                 {

  259.                         if(debug == 1)

  260.                                 {

  261.                         GET_LOG(LogLevel[1],DEBUG,"DEBUG:handle_data() first time is info %s\n","no old data");

  262.                         printf("MYMON: handle_data() first time is info %s\n","no old data");

  263.                                 }

  264.                 }

  265.         if(seq != 0 ) //newdata and olddata have data

  266.                 {

  267.                         if(sub_store_data(datap,&last_data,row_count,&lenth) != 0 ) //store data to array

  268.                                 {

  269.                                         ret = -6;

  270.                                         GET_LOG(LogLevel[4],ERROR,"ERROR:handle_data() sub_store_data() error:%s\n","ERROR");

  271.                                         printf("MYMON: handle_data() sub_store_data() error %s\n","ERROR");

  272.                                         return ret;

  273.                                 }

  274.                 if(debug == 1)

  275.                         {

  276.                         tmp_last_data = last_data;

  277.                         for(tmp =0;tmp<lenth;tmp++)

  278.                                 {

  279.                                         GET_LOG(LogLevel[1],DEBUG,"DEBUG:handle_data() find data is [%s]",(tmp_last_data)->statname);

  280.                                         GET_LOG(LogLevel[1],DEBUG,"[%lld] \n",(tmp_last_data)->data);

  281.                                         tmp_last_data++;

  282.                                 }

  283.                         }




  284.                         if(print_data(last_data,lenth,fd) != 0) //store data from array

  285.                                 {

  286.                                         ret = -7;

  287.                                         GET_LOG(LogLevel[4],ERROR,"ERROR:handle_data() print_data() error:%s\n","ERROR");

  288.                                         printf("MYMON: handle_data() print_data() error %s\n","ERROR");

  289.                                         return ret;

  290.                                 }


  291.                 }

  292.         //used end free last data array

  293.         xfree(last_data);

  294.         return ret;

  295. }



  296. int sub_store_data(MDATA* *datap,D_STROE* *last_data,uint64_t row_count,int* lenth)

  297. {

  298.         D_STROE* tmp_p = NULL;

  299.         int ret = 0;

  300.         uint64_t i = 0;


  301.         char* *dyd_d = NULL;

  302.         char* *std_d = NULL;

  303.         int64_t a = 0;

  304.         int64_t b = 0;


  305.         if(datap == NULL || last_data == NULL )

  306.                 {

  307.                     ret = -1;

  308.                         GET_LOG(LogLevel[4],ERROR,"ERROR:sub_store_data() datap == NULL || fail_data == NULL %s\n","ERROR");

  309.                         printf("MYMON: sub_store_data() datap == NULL || fail_data == NULL %s\n","ERROR");

  310.                         return ret;

  311.                 }


  312.         if((tmp_p = (D_STROE*)calloc(1,(sizeof(stdata)+sizeof(dydata))*sizeof(D_STROE))) == NULL)

  313.                 {

  314.                         ret = -2;

  315.                         GET_LOG(LogLevel[4],ERROR,"ERROR:sub_store_data()  mem alloc error:%s\n","ERROR");

  316.                         printf("MYMON: sub_store_data()  mem alloc error:%s\n","ERROR");

  317.                         return ret;

  318.                 }

  319.         *last_data = tmp_p;


  320.         for(i=0;i<row_count;i++)

  321.                 {

  322.                         dyd_d = dydata; //re pointer to dydata

  323.                 std_d = stdata; //re pointer to dydata

  324.                         if(strlen((*(datap+i))->statname)>MAXSTATNAME)

  325.                                 {

  326.                                         ret = -3;

  327.                                         GET_LOG(LogLevel[4],ERROR,"ERROR:sub_store_data()  statname>MAXSTATNAME:%s\n","ERROR");

  328.                                         printf("MYMON: sub_store_data()  statname>MAXSTATNAME:%s\n","ERROR");

  329.                                         goto er;

  330.                                 }

  331.                         while(*dyd_d)

  332.                                 {

  333.                                         if(debug == 3)

  334.                                                 {

  335.                                                         printf("dyd [%s] [%s] [%d] [%d]\n",*dyd_d,(*(datap+i))->statname,strlen(*dyd_d),strlen((*(datap+i))->statname));

  336.                                                 }

  337.                                         if(!strcmp(*dyd_d,(*(datap+i))->statname))

  338.                                                 {


  339.                                                         if(debug == 1)

  340.                                                                 {

  341.                                                                         GET_LOG(LogLevel[1],DEBUG,"DEBUG:sub_store_data() find data is %s  \n",*dyd_d);

  342.                                                                 }

  343.                                                         strcpy(tmp_p->statname,*dyd_d);

  344.                                                         if( !strcmp((*(datap+i))->olddata,"NULL") || !strcmp((*(datap+i))->newdata,"NULL")) //check "NULL" DATA ?

  345.                                                                 {

  346.                                                                         GET_LOG(LogLevel[2],INFO,"INFO:sub_store_data() data %s is [NULL] \n",*dyd_d);

  347.                                                                         tmp_p->data = ERRORDATA;

  348.                                                                 }

  349.                                                         if(sscanf((*(datap+i))->olddata,"%lld",&a) == EOF || sscanf((*(datap+i))->newdata,"%lld",&b) == EOF)

  350.                                                                 {

  351.                                                                         ret = -4;

  352.                                                                         GET_LOG(LogLevel[4],ERROR,"ERROR:sub_store_data() sscanf() error %s\n","ERROR");

  353.                                                                         printf("MYMON: sub_store_data() sscanf() error:%s\n","ERROR");

  354.                                                                         goto er;

  355.                                                                 }

  356.                                                         tmp_p->data = b-a; //new - old

  357.                                                         tmp_p++;

  358.                                                         (*lenth)++;

  359.                                                         break;

  360.                                                 }

  361.                                         dyd_d++;

  362.                                 }

  363.                         while(*std_d)

  364.                                 {

  365.                                         if(debug == 3)

  366.                                                 {

  367.                                                         printf("std [%s] [%s] [%d] [%d]\n",*std_d,(*(datap+i))->statname,strlen(*std_d),strlen((*(datap+i))->statname));

  368.                                                 }

  369.                                         if(!strcmp(*std_d,(*(datap+i))->statname))

  370.                                                 {


  371.                                                      if(debug == 1)

  372.                                                                 {

  373.                                                                         GET_LOG(LogLevel[1],DEBUG,"DEBUG:sub_store_data() find data is %s \n",*std_d);

  374.                                                                 }

  375.                                                         strcpy(tmp_p->statname,*std_d);

  376.                                                         if(!strcmp((*(datap+i))->newdata,"NULL")) //check "NULL" DATA ?

  377.                                                                 {

  378.                                                                         GET_LOG(LogLevel[2],INFO,"INFO:sub_store_data() data %s is [NULL] \n",*std_d);

  379.                                                                         tmp_p->data = ERRORDATA;

  380.                                                                 }

  381.                                                         if(sscanf((*(datap+i))->olddata,"%lld",&tmp_p->data) == EOF)

  382.                                                                 {

  383.                                                                         ret = -4;

  384.                                                                         GET_LOG(LogLevel[4],ERROR,"ERROR:sub_store_data() sscanf() error %s\n","ERROR");

  385.                                                                         printf("MYMON: sub_store_data() sscanf() error:%s\n","ERROR");

  386.                                                                         goto er;

  387.                                                                 }

  388.                                                         tmp_p++;

  389.                                                         (*lenth)++;

  390.                                                         break;

  391.                                                 }

  392.                                 std_d++;

  393.                                 }

  394.                 }

  395.         return ret;


  396.         er:

  397.                 xfree(tmp_p);

  398.                 *last_data= NULL;

  399.                 return ret;

  400. }


  401. int print_data(D_STROE* last_data,int lenth,FILE* fd)

  402. {

  403.         time_t rawtime;

  404.         struct tm * timeinfo = NULL;

  405.         int ret = 0;

  406.         FILE* outfile = NULL;

  407.         if(write_file == 1)

  408.                 {

  409.                         outfile=fd;

  410.                 }

  411.         else

  412.                 {

  413.                         outfile=stdout;

  414.                 }


  415.         if(time (&rawtime) == -1)

  416.                 {

  417.                         ret = -2;

  418.                         GET_LOG(LogLevel[4],ERROR,"ERROR:print_data() time() error %s\n","ERROR");

  419.             printf("MYMON: ERROR:print_data() time() error:%s\n","ERROR");

  420.                         return ret;

  421.                 }

  422.         timeinfo = localtime ( &rawtime );

  423.         if (timeinfo == NULL )

  424.                 {

  425.                         ret = -1;

  426.                         GET_LOG(LogLevel[4],ERROR,"ERROR:print_data() localtime () error %s\n","ERROR");

  427.                         printf("MYMON: ERROR:print_data() localtime () error:%s\n","ERROR");

  428.                         return ret;

  429.                 }

  430.         mprintf(outfile,"------------------------------------------MYMON--------------------------------------------\n");

  431.         mprintf(outfile,"SysTime: %s",asctime (timeinfo));

  432.         mprintf(outfile,

  433.                     "%s%-11ld%s%-8ld%s%-8ld%s%-8ld\n",

  434.                         "Uptime:",findva("Uptime",last_data,lenth),

  435.                         "Connections:",findva("Connections",last_data,lenth) ,

  436.                         "Aborted_clients:",findva("Aborted_clients",last_data,lenth),

  437.                         "Max_used_connections:",findva("Max_used_connections",last_data,lenth));

  438.         mprintf(outfile,"--------------------------------------MYSQLINFO-------------------------------------------\n");

  439.         mprintf(outfile,"[###Threads info and qc info:]\n");

  440.         mprintf(outfile,

  441.                     "%s%-12ld%s%-14ld%s%-13ld\n",

  442.                    "Threads_connected:",findva("Threads_connected",last_data,lenth),

  443.            "Threads_running:",findva("Threads_running",last_data,lenth),

  444.            "Qcache_free_memory:",findva("Qcache_free_memory",last_data,lenth)

  445.                         );

  446.         mprintf(outfile,

  447.                     "%s%-18ld%s%-12ld%s%-17ld\n",

  448.                    "Qcache_hits:",findva("Qcache_hits",last_data,lenth),

  449.                    "Qcache_not_cached:",findva("Qcache_not_cached",last_data,lenth),

  450.                    "Qcache_inserts:",findva("Qcache_inserts",last_data,lenth)

  451.                   );

  452.         mprintf(outfile,"[###Table read info:]\n");

  453.         mprintf(outfile,

  454.                     "%s%-11ld%s%-13ld%s%-14ld\n",

  455.                         "Handler_read_first:",findva("Handler_read_first",last_data,lenth),

  456.                         "Handler_read_key:",findva("Handler_read_key",last_data,lenth),

  457.                         "Handler_read_next:",findva("Handler_read_next",last_data,lenth)

  458.                         );


  459.         mprintf(outfile,

  460.                     "%s%-12ld%s%-13ld%s%ld\n",

  461.                         "Handler_read_last:",findva("Handler_read_last",last_data,lenth),

  462.                         "Handler_read_rnd:",findva("Handler_read_rnd",last_data,lenth),

  463.                         "Handler_read_rnd_next:",findva("Handler_read_rnd_next",last_data,lenth)

  464.                         );

  465.         mprintf(outfile,"[###Sorts and temp tables info:]\n");

  466.         mprintf(outfile,

  467.                     "%s%-20ld%s%ld\n",

  468.                     "Sort_rows:",findva("Sort_rows",last_data,lenth),

  469.                         "Sort_merge_passes:",findva("Sort_merge_passes",last_data,lenth)

  470.                         );

  471.         mprintf(outfile,

  472.                     "%s%-11ld%s%ld\n",

  473.                         "Created_tmp_tables:",findva("Created_tmp_tables",last_data,lenth),

  474.                         "Created_tmp_disk_tables:",findva("Created_tmp_disk_tables",last_data,lenth)

  475.                         );

  476.         mprintf(outfile,"[###Table join info:]\n");

  477.     mprintf(outfile,

  478.                     "%s%-13ld%s%ld\n",

  479.                         "Select_full_join:",findva("Select_full_join",last_data,lenth),

  480.                         "Select_scan:",findva("Select_scan",last_data,lenth)

  481.                         );

  482.          

  483.           mprintf(outfile,"[###Open table cache and binlog cache info:]\n");

  484.           mprintf(outfile,

  485.                     "%s%-8ld%s%ld\n",

  486.                         "Table_open_cache_hits:",findva("Table_open_cache_hits",last_data,lenth),

  487.                         "Table_open_cache_misses:",findva("Table_open_cache_misses",last_data,lenth)

  488.                         );

  489.           mprintf(outfile,

  490.                     "%s%-13ld%s%ld\n",

  491.                         "Binlog_cache_use:",findva("Binlog_cache_use",last_data,lenth),

  492.                         "Binlog_cache_disk_use:",findva("Binlog_cache_disk_use",last_data,lenth)

  493.                         );

  494.          mprintf(outfile,"[###Hanler_xxx and Com_xxx info:]\n");


  495.          mprintf(outfile,

  496.                     "%s%-15ld%s%-15ld%s%ld\n",

  497.                         "Handler_delete:",findva("Handler_delete",last_data,lenth),

  498.                         "Handler_update:",findva("Handler_update",last_data,lenth),

  499.                         "Handler_write:",findva("Handler_write",last_data,lenth)

  500.                         );

  501.          mprintf(outfile,

  502.                     "%s%-15ld%s%-ld\n",

  503.                         "Handler_commit:",findva("Handler_commit",last_data,lenth),

  504.                         "Handler_rollback:",findva("Handler_rollback",last_data,lenth)

  505.                         );

  506.           mprintf(outfile,

  507.                          "%s%-19ld%s%ld\n",

  508.                          "Com_commit:",findva("Com_commit",last_data,lenth),

  509.                          "Com_rollback:",findva("Com_rollback",last_data,lenth)

  510.                          );

  511.         mprintf(outfile,

  512.                     "%s%-8ld%s%-8ld%s%-8ld%s%-8ld\n",

  513.                         "U(counts):",findva("Com_update",last_data,lenth),

  514.                         "D(counts):",findva("Com_delete",last_data,lenth),

  515.                         "I(counts):",findva("Com_insert",last_data,lenth),

  516.                         "S(counts):",findva("Com_select",last_data,lenth)

  517.                         );

  518.     mprintf(outfile,

  519.                    "--------------------------------------INNODBINFO------------------------------------------\n");

  520.     mprintf(outfile,"[###Innodb_lock info:]\n");

  521.         mprintf(outfile,

  522.                     "%s%-17ld%s%ld\n",

  523.                         "Innodb_row_lock_time:",findva("Innodb_row_lock_time",last_data,lenth),

  524.                         "Innodb_row_lock_waits:",findva("Innodb_row_lock_waits",last_data,lenth)

  525.                         );

  526.         mprintf(outfile,

  527.                     "%s%ld\n",

  528.                         "Innodb_row_lock_current_waits:",findva("Innodb_row_lock_current_waits",last_data,lenth)

  529.                         );

  530.     mprintf(outfile,"[###Innodb_buffer_pool info:]\n");


  531.         mprintf(outfile,"%s%-10ld%s%ld\n","Innodb_buf_pool_pages_total:",findva("Innodb_buffer_pool_pages_total",last_data,lenth)

  532.                     ,"Innodb_buf_pool_pages_dirty:",findva("Innodb_buffer_pool_pages_dirty",last_data,lenth));


  533.         mprintf(outfile,"%s%-16ld%s%ld\n","Innodb_buf_pages_data:",findva("Innodb_buffer_pool_pages_data",last_data,lenth)

  534.                     ,"Innodb_buf_pages_free:",findva("Innodb_buffer_pool_pages_free",last_data,lenth));


  535.         mprintf(outfile,

  536.                     "%s%-9ld%s%ld\n",

  537.                         "Innodb_buffer_pool_wait_free:",findva("Innodb_buffer_pool_wait_free",last_data,lenth),

  538.                         "Innodb_buffer_pool_pages_misc:",findva("Innodb_buffer_pool_pages_misc",last_data,lenth)

  539.                         );

  540.         mprintf(outfile,

  541.                     "%s%-11ld%s%ld\n",

  542.                         "Innodb_dblwr_pages_written:",findva("Innodb_dblwr_pages_written",last_data,lenth),

  543.                         "Innodb_dblwr_writes:",findva("Innodb_dblwr_writes",last_data,lenth)

  544.                         );


  545.         mprintf(outfile,

  546.                     "%s%ld\n",

  547.                         "Innodb_buffer_pool_pages_flushed:",findva("Innodb_buffer_pool_pages_flushed",last_data,lenth)

  548.                         );

  549.         mprintf(outfile,

  550.                      "%s%-10ld %s%-10ld %s%-10ld\n",

  551.                      "read_ahead:",findva("Innodb_buffer_pool_read_ahead",last_data,lenth),

  552.                      "ahead_evict:",findva("Innodb_buffer_pool_read_ahead_evicted",last_data,lenth),

  553.                      "ahead_rnd:",findva("Innodb_buffer_pool_read_ahead_rnd",last_data,lenth));

  554.         mprintf(outfile,"[###Innodb_log info:]\n");

  555.         mprintf(outfile,

  556.                      "%s%-20ld%s%ld\n",

  557.                         "Innodb_log_writes:",findva("Innodb_log_writes",last_data,lenth),

  558.                         "Innodb_log_waits:",findva("Innodb_log_waits",last_data,lenth)

  559.                         );

  560.         mprintf(outfile,

  561.                     "%s%-9ld%s%ld\n",

  562.                         "Innodb_os_log_pending_fsyncs:",findva("Innodb_os_log_pending_fsyncs",last_data,lenth),

  563.                         "Innodb_os_log_pending_writes:",findva("Innodb_os_log_pending_writes",last_data,lenth)

  564.                         );

  565.         mprintf(outfile,

  566.                     "%s%ld\n",

  567.                         "Innodb_os_log_written:",findva("Innodb_os_log_written",last_data,lenth)

  568.                    );

  569.         mprintf(outfile,"[###Innodb_data read/writes info:]\n");

  570.     mprintf(outfile,

  571.                     "%s%-20ld%s%ld\n",

  572.                         "Innodb_data_reads:",findva("Innodb_data_reads",last_data,lenth),

  573.                         "Innodb_data_read:",findva("Innodb_data_read",last_data,lenth)

  574.                         );

  575.     mprintf(outfile,

  576.                     "%s%-19ld%s%ld\n",

  577.                         "Innodb_data_writes:",findva("Innodb_data_writes",last_data,lenth),

  578.                         "Innodb_data_written:",findva("Innodb_data_written",last_data,lenth)

  579.                         );

  580.         mprintf(outfile,

  581.                     "%s%-19ld%s%ld\n",

  582.                     "Innodb_data_fsyncs:",findva("Innodb_data_fsyncs",last_data,lenth),

  583.                     "Innodb_data_pending_fsyncs:",findva("Innodb_data_pending_fsyncs",last_data,lenth)

  584.                     );

  585.         mprintf(outfile,

  586.                     "%s%ld\n",

  587.                         "Innodb_buffer_pool_reads(physics reads):",findva("Innodb_buffer_pool_reads",last_data,lenth)

  588.                    );

  589.         mprintf(outfile,

  590.                     "%s%ld\n",

  591.                         "Innodb_buffer_pool_read_requests(logic reads):",findva("Innodb_buffer_pool_read_requests",last_data,lenth)

  592.                    );


  593.         mprintf(outfile,"[###Innodb_data rows info:]\n");

  594.         mprintf(outfile,

  595.                     "%s%-8ld%s%-8ld%s%-8ld%s%-8ld\n",

  596.                         "U(rows):",findva("Innodb_rows_updated",last_data,lenth),

  597.                         "D(rows):",findva("Innodb_rows_deleted",last_data,lenth),

  598.                         "I(rows):",findva("Innodb_rows_inserted",last_data,lenth),

  599.                         "S(rows):",findva("Innodb_rows_read",last_data,lenth)

  600.                         );


  601.         mprintf(outfile,"---------------------------------------TOTALINFO------------------------------------------\n");

  602.         mprintf(outfile,

  603.                     "%s%-15ld%s%ld\n",

  604.                         "Bytes_received:",findva("Bytes_received",last_data,lenth),

  605.                         "Bytes_sent:",findva("Bytes_sent",last_data,lenth)

  606.                         );

  607.         return ret;

  608. }


  609. int64_t findva(const char* ins,D_STROE* last_data,int lenth)

  610. {

  611.         int i=0;

  612.         for(i=0;i<lenth;i++)

  613.                 {

  614.                         if(!strcmp(last_data->statname,ins))

  615.                                 {

  616.                                         return last_data->data;

  617.                                         break;

  618.                                 }

  619.                         last_data++;

  620.                 }

  621.         return (int64_t)(-1);

  622. }

以上是“如何使用自己开发的MYMON工具监控MYSQL运行状态”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网数据库频道!

您可能感兴趣的文档:

--结束END--

本文标题: 如何使用自己开发的MYMON工具监控MYSQL运行状态

本文链接: https://www.lsjlt.com/news/62665.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何使用自己开发的MYMON工具监控MYSQL运行状态
    这篇文章主要介绍如何使用自己开发的MYMON工具监控MYSQL运行状态,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、此工具展示方式:     工具将...
    99+
    2022-10-18
  • 如何使用JDK自带的jmap和jhat监控处于运行状态的Java进程
    本篇内容主要讲解“如何使用JDK自带的jmap和jhat监控处于运行状态的Java进程”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用JDK自带的jmap和jhat监控处于运行状态的Jav...
    99+
    2023-06-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作