前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux 性能监控

linux 性能监控

原创
作者头像
葫芦
修改2019-04-13 13:33:47
4.2K0
修改2019-04-13 13:33:47
举报
文章被收录于专栏:葫芦

示例:点击 -> 性能监控

先上效果:

内存监控:

/proc/meminfo used=total-(buffers+cached+free)

代码语言:txt
复制
[root@wangzi go]# cat /proc/meminfo
MemTotal:        1016212 kB
MemFree:           77724 kB
MemAvailable:      51692 kB
Buffers:            8468 kB
Cached:            59584 kB

python监控代码:

代码语言:javascript
复制
#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com

def mem_use():
    free=0
    used=0
    total=0
    buffers=0
    cached=0
    with open('/proc/meminfo','r') as file:
        for line in file.readlines():
            if line.startswith('MemTotal:'):
                total=line.split()[1]
            if line.startswith('MemFree:'):
                free=line.split()[1]
            if line.startswith('Cached:'):
                cached=line.split()[1]
            if line.startswith('Buffers:'):
                buffers=line.split()[1]
    used=int(total)-int(free)-int(cached)-int(buffers)
    insert_sql="""insert into `monitor_mem_use` (`used`,`free`,`cached`,`buffers`,`flow_time`) values ({0},{1},{2},{3},'{4}')""".format(used,free,cached,buffers,now_zero)
    cursorUpdate(insert_sql,[])

网卡IO监控:

通过/proc/net/dev获取网卡 IO,第1列和第9列分别代表了in 和out。

代码语言:javascript
复制
[root@wangzi go]#  cat /proc/net/dev
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
  eth0: 10020876640 52702968    0    0    0     0          0         0 11473861781 54530293    0    0    0     0       0          0
    lo: 6454958709 6187408    0    0    0     0          0         0 6454958709 6187408    0    0    0     0       0          0

python代码:

代码语言:javascript
复制
#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com


def write_net_io():
    net_in=0
    net_out=0
    add_net_in=0
    add_net_out=0
    with open('/proc/net/dev', 'r') as file:
        for line in file.readlines():
            if 'eth0' in line:
                net_in=int(line.split()[1])/1024
                net_out=int(line.split()[9])/1024
    select_sql="""select `in`,`out` from monitor_net_io order by id desc limit 1"""
    result=cursorQuery(select_sql,[])
    if len(result)>0:
        add_net_in=net_in-int(result[0][0])
        add_net_out=net_out-int(result[0][1])
    insert_sql="""insert into `monitor_net_io` (`in`,`out`,`add_in`,`add_out`,`flow_time`) values ({0},{1},{2},{3},'{4}')""".format(net_in,net_out,add_net_in,add_net_out,now_zero)
    cursorUpdate(insert_sql,[])

CPU监控:

代码语言:javascript
复制
[root@wangzi go]# cat /proc/stat
cpu  25187586 4339 20108620 1703341684 3875717 0 58452 0 0 0

pytho代码:

代码语言:javascript
复制
#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com


def cpu_use():
    use=0
    with open('/proc/stat','r') as file:
        for line in file.readlines():
            if line.startswith('cpu ',0,4):
                use=round((1-float(line.split()[4])/sum([int(x) for x in line.split()[1:]]))*100,5)
    insert_sql="""insert into `monitor_cpu_use` (`use`,`flow_time`) values ({0},'{1}')""".format(use,now_zero)
    cursorUpdate(insert_sql,[])

句柄监控:

/proc/sys/fs/file-nr 每列分别代表:

已分配文件句柄的数目 已使用文件句柄的数目 文件句柄的最大数目

代码语言:javascript
复制
[root@wangzi go]# cat /proc/sys/fs/file-nr
1280	0	98406

python监控代码:

代码语言:javascript
复制
#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com

def fd_use():
    free=0
    used=0
    total=0
    with open('/proc/sys/fs/file-nr','r') as file:
        for line in file.readlines():
            used=int(line.split()[0])
            total=int(line.split()[2])
    free=total-used
    insert_sql="""insert into `monitor_fd_use` (`used`,`free`,`flow_time`) values ({0},{1},'{2}')""".format(used,free,now_zero)
    cursorUpdate(insert_sql,[])

TCP连接监控:

/proc/net/tcp 第四列 01代表了 TCP_ESTABLISHED 06代表代表time_wait 08代表close_wait

代码语言:javascript
复制
[root@wangzi ~]# cat /proc/net/tcp| awk '{if($4 == '01') print $0}'|wc -l
22
[root@wangzi ~]# netstat -antpl|grep ESTABLISHED|wc -l
22
[root@wangzi ~]#cat /proc/net/tcp| awk '{if($4 == '01' || $4=='06' || $4=='08') print $4}'

因使用netstat命令有时会占用较多资源,当机器负载较高时,可以用使用上面的命令来查看tcp正在通信的连接数。

python监控:

代码语言:javascript
复制
#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com


def net_tcp():
    ret=Popen("""cat /proc/net/tcp| awk '{if($4 == '01' || $4=='06' || $4=='08') print $4}'""",shell=True,stdout=PIPE)
    established=0
    time_wait=0
    close_wait=0
    for item in ret.stdout.readlines():
       if int(item)==1:
           established+=1
       elif int(item)==6:
           time_wait+=1
       elif int(item)==6:
           close_wait+=1
    insert_sql="""insert into `monitor_net_tcp` (`establish`,`time_wait`,`close_wait`,`flow_time`) values ({0},{1},{2},'{3}')""".format(established,time_wait,close_wait,now_zero)
    cursorUpdate(insert_sql,[])

磁盘IO监控:

固定时间段监控/proc/vmstat 以下字段 和前一个时间段相减,再除以时间间隔 得到io。

代码语言:javascript
复制
[root@wangzi teg]# cat /proc/vmstat|grep pgpg
pgpgin 2094314778
pgpgout 265255600

python代码:

代码语言:javascript
复制
#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com


def write_disk_io():
    disk_in=0
    disk_out=0
    with open('/proc/vmstat', 'r') as file:
        for line in file.readlines():
            if 'pgpgin' in line:
                disk_in=int(line.split()[1])/1024
            if 'pgpgout' in line:
                disk_out=int(line.split()[1])/1024
    select_sql="""select `in`,`out` from monitor_disk_io order by id desc limit 1"""
    result=cursorQuery(select_sql,[])
    add_disk_in=disk_in-int(result[0][0])
    add_disk_out=disk_out-int(result[0][1])
    insert_sql="""insert into `monitor_disk_io` (`in`,`out`,`add_in`,`add_out`,`flow_time`) values ({0},{1},{2},{3},'{4}')""".format(disk_in,disk_out,add_disk_in,add_disk_out,now_zero)
    cursorUpdate(insert_sql,[])

分区空间使用率监控:

通过df命令统计:

代码语言:javascript
复制
[root@wangzi go]# df |grep -v ^Filesystem|awk -F ' ' '{print $6"|"$3"|"$4}'
/|6999556|42168392
/dev|0|498596
/dev/shm|24|508080
/run|412|507692
/sys/fs/cgroup|0|508104
/run/user/0|0|101624

python代码:

代码语言:javascript
复制
#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com


from subprocess import Popen,PIPE
def disk_df():
    ret=Popen("""df |grep -v ^Filesystem|awk -F ' ' '{print $6"|"$3"|"$4}'""",shell=True,stdout=PIPE)
    for item in ret.stdout.readlines():
        disk_info=item.replace('\n','').split('|')
        insert_sql="""insert into `monitor_disk_df` (`path`,`used`,`idle`,`flow_time`) values ('{0}',{1},{2},'{3}')""".format(disk_info[0],disk_info[1],disk_info[2],now_zero)
        cursorUpdate(insert_sql,[])

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 示例:点击 -> 性能监控
  • 先上效果:
    • 内存监控:
    • 网卡IO监控:
    • CPU监控:
    • 句柄监控:
    • TCP连接监控:
    • 磁盘IO监控:
    • 分区空间使用率监控:
    相关产品与服务
    应用性能监控
    应用性能监控(Application Performance Management,APM)是一款应用性能管理平台,基于实时多语言应用探针全量采集技术,为您提供分布式性能分析和故障自检能力。APM 协助您在复杂的业务系统里快速定位性能问题,降低 MTTR(平均故障恢复时间),实时了解并追踪应用性能,提升用户体验。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档