3月 08

linux下查看使用硬盘IO过高的进程

  服务器cpu使用率不高,load比较高,所以要查看一下IO。硬盘IO可以通过命令vmstat或iostat获得(也可以用yum 安装dstat获得),网络IO可以用iftop命令获取。但是不知道那个进程使用硬盘IO比较高,通过查找没有找到相关命令,只好自己写个脚本进行统计处理。
  本脚本在CentOS6下(kernel2.6以上)python2.6测试通过。
  直接运行脚本,默认情况下收集3秒钟数据,显示读写最高的前三个进程。如用参数可以使用命令“python fhip.py 4 5 3”,第一个数位每次收集读写数据的间隔秒数,第二个数是打印出读写最多的n个进程,第三个为运行脚本的次数。因为参数部分写的比较简单那,所以用参数必须3个全写。

#!/bin/python 
#-*- coding:utf-8 -*- 
# Filename:    find_high_io_process
# Revision:    1.0 
# Date:        2013-3-8 
# Author:      simonzhang 
# web:         www.simonzhang.net 
# Email:       simon-zzm@163.com 
### END INIT INFO
import os
import re
import sys
import time
from string import strip

####
sys_proc_path = '/proc/'
re_find_process_number = '^\d+$'

####
# 通过/proc/$pid/io获取读写信息
####
def collect_info():
    _tmp = {}
    re_find_process_dir = re.compile(re_find_process_number)
    for i in os.listdir(sys_proc_path):
        if re_find_process_dir.search(i):
            # 获得进程名
            process_name = open("%s%s/stat" % (sys_proc_path, i), "rb").read().split(" ")[1]
            # 读取io信息
            rw_io = open("%s%s/io" % (sys_proc_path, i), "rb").readlines()
            for _info in rw_io:
                cut_info = strip(_info).split(':')
                if strip(cut_info[0]) == "read_bytes":
                    read_io = int(strip(cut_info[1]))
                if strip(cut_info[0]) == "write_bytes":
                    write_io = int(strip(cut_info[1]))
            _tmp[i] = {"name":process_name, "read_bytes":read_io, "write_bytes":write_io}
    return _tmp


def main(_sleep_time, _list_num):
    _sort_read_dict = {}
    _sort_write_dict = {}
    # 获取系统读写数据
    process_info_list_frist = collect_info()
    time.sleep(_sleep_time)
    process_info_list_second = collect_info()
    # 将读数据和写数据进行分组,写入两个字典中
    for loop in process_info_list_second.keys():
        second_read_v = process_info_list_second[loop]["read_bytes"]
        second_write_v = process_info_list_second[loop]["write_bytes"]
        try:
            frist_read_v = process_info_list_frist[loop]["read_bytes"]
        except:
            frist_read_v = 0
        try:
            frist_write_v = process_info_list_frist[loop]["write_bytes"]
        except:
            frist_write_v = 0
        # 计算第二次获得数据域第一次获得数据的差
        _sort_read_dict[loop] = second_read_v - frist_read_v
        _sort_write_dict[loop] = second_write_v - frist_write_v
    # 将读写数据进行排序
    sort_read_dict = sorted(_sort_read_dict.items(),key=lambda _sort_read_dict:_sort_read_dict[1],reverse=True)
    sort_write_dict = sorted(_sort_write_dict.items(),key=lambda _sort_write_dict:_sort_write_dict[1],reverse=True)
    # 打印统计结果
    print "pid     process     read(bytes) pid     process     write(btyes)"
    for _num in range(_list_num):
        read_pid = sort_read_dict[_num][0]
        write_pid = sort_write_dict[_num][0]
        res = "%s" % read_pid
        res += " " * (8 - len(read_pid)) + process_info_list_second[read_pid]["name"]
        res += " " * (12 - len(process_info_list_second[read_pid]["name"])) + "%s" % sort_read_dict[_num][1]
        res += " " * (12 - len("%s" % sort_read_dict[_num][1])) + write_pid
        res += " " * (8 - len(write_pid)) + process_info_list_second[write_pid]["name"]
        res += " " * (12 - len("%s" % process_info_list_second[write_pid]["name"])) + "%s" % sort_write_dict[_num][1]
        print res
    print "\n" * 1


if __name__ == '__main__':
    try:
        _sleep_time = sys.argv[1]
    except:
        _sleep_time = 3
    try:
        _num = sys.argv[2]
    except:
        _num = 3
    try:
        loop = sys.argv[3]
    except:
        loop = 1
    for i in range(int(loop)):
        main(int(_sleep_time), int(_num))

linux查找IO高的进程的源码

3月 19

python 脚本不能并行运行

        写了个脚本,数据需要定时循环处理,但是不能同时重复处理。也就是说,脚本需要单进程运行。脚本定时运行配置在crontab中。如果在脚本开始写状态文件,运行完成后关闭状态文件也可以,但是如果脚本中途退出,状态文件不会更改,下次运行就不容易判断了。使用进程号来判断就比较准确了,所以写了以下代码。
       以下脚本为linux下使用。

#!/usr/local/bin/python
# -------------------------------------------------------------------------------
# Filename:    my-pid.py
# Revision:
# Date:        2012-12-19
# Author:      simonzhang
# Email:       simon-zzm@163.com
# -------------------------------------------------------------------------------
import os

if __name__ == '__main__':
    try :
        #首先查看是否有pid文件
        #读取pid
        now_pid = open("./my_pid.pid","rb").read()
        #通过操作系统命令,统计pid运行的数量
        get_count = os.popen("ps -ef|awk ' ''{print $2}'|grep -w %s|wc -l"%now_pid).read()
    except:
        #如果没有pid文件,则统计为零
        get_count = 0
    #判断pid是否在进程中
    if int(get_count) > 0 :
        #如果系统进程中有pid进程,则打印后推出
        print "run...."
        sys.exit()
    else:
        #如果没有进程则获得后,保存在pid文件中
        w_pid = os.getpid()
        w_pid_file = open("./my_pid.pid","wb")
        w_pid_file.write("%s"%w_pid)
        w_pid_file.close()
        #运行脚本
         main()
10月 04

python 链接 redis 测试

[2011-9-11 张子萌]
[2012-5-27 张子萌 修改]
  安装支持的模块,简单的方法是使用easy_install
# easy_install redis

  也可以下载安装包手动安装。做个简单的写入测试,使用有664039行的密码表,将密码放到redis中。代码如下:

#!/usr/bin/env python
import redis
def main():
    r = redis.Redis(host='192.168.1.200', port=6379, db=0)
    w_pass=open('big_pass.txt','r').readlines()
    next_line=0
    count_lines=len(w_pass)
    while next_line+1 < count_lines:
        r.set(str(next_line),w_pass[next_line])
        next_line=next_line+1
    print 'Game over!'
if __name__=='__main__':
    main()

测试服务器为一个Xeon(TM)双核 3.20GHz 的 cpu,内存4G。
a)写入
用两台机器做测试。两台机器均在一个千兆局域网内。实验很简单所以只记录了cpu的负载和脚本运行时间。
# time python test_redis1.py
Game over!
real 4m16.878s
user 0m55.910s
sys 0m16.879s

cpu部分
远端机缓存 load average: 1.46, 1.26, 1.34

Redis服务器 load average: 0.52, 0.30, 0.22

单台服务器本地测试:
# time python python_redis.py
Game over!
real 1m32.065s
user 1m2.643s
sys 0m17.890s

cpu部分
load average: 1.32, 0.57, 0.32

b)存环从reids中读取所有数据,测试结果如下:
两台服务器:
# time python test_redis1.py
Game over!

real 4m32.776s
user 1m3.687s
sys 0m17.675s

Redis服务器Cpu部分
load average: 0.90, 0.96, 0.69
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
27525 root 15 0 117m 110m 712 S 7.3 2.8 2:55.36 redis-server

单台服务器本地测试:
# time python python_redis.py
Game over!

real 1m44.790s
user 1m9.294s
sys 0m19.031s

Cpu部分
load average: 1.36, 0.92, 0.63

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3255 root 18 0 153m 47m 2640 R 87.1 1.2 0:29.43 python
27525 root 15 0 117m 110m 712 S 33.1 2.8 2:29.26 redis-server

Redis的其它操作并未测试,但是从当前测试看,单进程每秒读写差不多再7000-8000,cpu使用维持在百分之三十多,确实效率不错。
注为了测试方便,我的redis服务没有加密码。如需加访问密码或有其它需参数,参考如下:
host='localhost'
port=6379
db=0
password=None
socket_timeout=None
connection_pool=None
charset='utf-8'
errors='strict'
unix_socket_path=None