4月 18

监控mysql从机同步状态脚本

  mysql数据库主从运行。为了知道从机的同步情况,写了个脚本,放在crontab中,如果同步出错,则邮件报警。去年写的,放上来做个备忘。

#!/usr/local/bin/python
# -------------------------------------------------------------------------------
# Filename:    .py
# Revision:    1.0
# Date:        2012-03-20
# Author:      simonzhang
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# -------------------------------------------------------------------------------
import os
import pexpect
import time
import smtplib
from email.mime.text import MIMEText

#### base se
mysql_bin = '/mysql5/bin/mysql'
mysql_user = ''
mysql_pass = ''
mail_host = 'smtp.exmail.qq.com'
mail_user = 'XXX@XXX.net'
mail_pwd = 'XXXX'
mail_to = "xxxxx@xxx.com"
####

def mail_warn(error_ip):
    content = 'IP %s mysql slave is error!'%error_ip
    msg = MIMEText(content)
    msg['From'] = mail_user
    msg['Subject'] = 'mysql warnning %s'%error_ip
    msg['To'] = mail_to
    try:
        s = smtplib.SMTP()
        s.connect(mail_host)
        s.login(mail_user,mail_pwd)
        s.sendmail(mail_user,[mail_to],msg.as_string())
        s.close()
    except Exception ,e:
        print e

def main():
    status = os.popen("%s -u%s -p%s -e 'show slave status\G'"%
                      (mysql_bin,mysql_user,mysql_pass)).read() 
    io_status = status[status.find('Slave_IO_Running: ')+18]
    sql_status = status[status.find('Slave_SQL_Running: ')+19]
    if (io_status == 'Y') or (sql_status == 'Y'):
        ip = os.popen("/sbin/ifconfig|grep 'inet addr'|awk '{print $2}'").read()
        get_local_ip = ip[ip.find(':')+1:ip.find('n')]
        mail_warn("%s"%get_local_ip)

if __name__ == "__main__":
    main()
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高的进程的源码

2月 21

服务器 自动封锁和解封IP 对付攻击

  服务器流量暴涨,首先通过访问日志查看。每个IP并发不是很高,分布也很广,但是都是只访问主页,可见不是抓取服务导致。通过“netstat -nat|awk ‘{print awk $NF}’|sort|uniq -c|sort -n”t命令查看,连接中还有很多SYN_RECV、TIME_WAIT、ESTABLISHED。看来是小型的攻击。首先是修改web服务的配置,缩减了timeout时间,在nginx上禁止一部分并发比较高的IP,小改系统参数。流量降了20M,但还是太高。nginx虽然禁止了IP,如果用浏览看是一个空白页面,但是每个连接还会产生很小的流量。最后决定将最小的返回值也封掉,直接在iptables上drop掉连接。每个超限IP暂定封2天。写个脚本自动运行。

  此脚本在在centos5.3+python2.6.6使用通过。需要注意,此脚本统计日志完毕会将nginx日志文件清空,如需保留日志,请自行修改。每次加载iptables规则时会清楚上次所有规则,如果有使用其它规则也要自行处理。

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


#### 参数和脚本中使用到的系统命令
nginx_log = "/usr/local/nginx/logs/nginx.access.log"
# 统计nginx日志中IP访问数量
check_comm = "/bin/cat %s |awk ' ''{print $1}'|sort |uniq -c|sort -n -k1 -r" % nginx_log
# 放在crontab中10分钟跑一次,访问超出n次的全部封掉
overproof = 3000
# 被封地址记录文件
# 文件中记录封IP时间和IP地址。时间单位为秒
lock_ip_list = "/usr/local/nginx/logs/lock_ip_list.txt"
# 被封地址解开时间。时间单位为秒
unlock_time = 3600*24*2 


def manage_lock_ip():
    # 获取当前时间
    get_now_time = int(time.time())
    # 管理日志字典
    man_ip = {}
    # 处理日志中的IP
    try:
        log_file = open('%s' % lock_ip_list, 'rb').readlines()
        for get_ip_info in log_file:
            _get_ip_info = strip(get_ip_info).split(' ')
            man_ip['%s' % _get_ip_info[1]] = int(_get_ip_info[0])
    except:
        exit(0)
    # 清空iptable列表,和ip记录日志
    os.popen('/sbin/iptables -F')
    clean_file = open('%s' % lock_ip_list, 'rb+')
    clean_file.truncate()
    # 开始处理IP,被封没有超时的IP写入iptables和日志中
    log_file = open('%s' % lock_ip_list, 'ab')
    for loop_ip in man_ip.keys():
        if (get_now_time - man_ip[loop_ip]) < unlock_time:
            os.popen('/sbin/iptables -I INPUT -s %s -j DROP' % loop_ip)
            log_file.write('%s %s\n' % (man_ip[loop_ip], loop_ip))
    log_file.close()
        


def main():
    # 已封IP地址字典
    drop_ip_list = {}
    # 加载已封IP日志
    try:
        log_file = open('%s' % lock_ip_list, 'rb').readlines()
        for get_drop_ip_info in log_file:
            _get_drop_ip_info = strip(get_drop_ip_info).split(' ')
            drop_ip_list['%s' % _get_drop_ip_info[1]] = int(_get_drop_ip_info[0])
    except:
        os.mknod('%s' % lock_ip_list)
    # 获取nginx日志中的访问超高的ip并写入日志
    access_high_ip = os.popen('%s' % check_comm).readlines()
    for get_ip_count in access_high_ip:
        try :
            _get_ip_count = strip(get_ip_count).split(' ')
            _get_ip = _get_ip_count[1]
            _get_count = _get_ip_count[0]
        except:
            pass
        if (int(_get_count) > int(overproof)) and (_get_ip not in drop_ip_list.keys()):
            now_time = int(time.time())
            log_file = open('%s' % lock_ip_list, 'ab+')
            log_file.write('%s %s\n' % (now_time, _get_ip))
            log_file.close()
    # 统计完毕清空nginx日志
    log_file = open('%s' % nginx_log, 'wb')
    log_file.truncate()
    # 处理要封的IP和要解开的IP
    manage_lock_ip()


if __name__ == '__main__':
    main()

再补充两条日志分析命令
# 单位时间内统计单个IP只访问首页的数量:
#check_comm = “/bin/cat %s|grep ‘GET / HTTP/1.1’|awk ‘ ”{print $1}’|sort |uniq -c|sort -n -k1 -r” % nginx_log
# 单位时间内统计单个IP访问相同页面的数量
#check_comm = “/bin/cat %s|awk -F'”‘ ‘{print $1 $2}’|awk ‘ ”{print $1″ “$6” “$7” “$8}’|sort -k2,4 -r|uniq -c|sort -n -k1 -r” % nginx_log

源码下载
drop_ip_tables

2月 05

raspberry pi 串口控制51单片机

  制作小车都用raspberry pi上的口有些麻烦,所以把一些基础控制给51单片机解决,raspberry pi控制高层应用。所以测试一下raspberry pi串口连接调试51单片机。
  第一步: 
sudo apt-get install python-serial
sudo easy_install pyserial

  第二部接线部分图
  raspberry pi板子串口接线图

raspberry 串口接线图1

raspberry 串口接线图1


  杜邦线连接
IMAG1302-1 
raspberry 串口接线图3

raspberry 串口接线图3

 

  代码部分
  raspberry pi python串口控制代码

#!/bin/env python
# -*- coding:utf-8 -*-
# -------------------------------
# Filename:    
# Revision:    
# Date:        2013-02-5
# Author:      simonzhang
# Email:       simon-zzm@163.com
# WWW:         www.simonzhang.net
# -------------------------------
import serial
import time

#### 定义小灯亮灭初始值
i = 0 
#### 实例化串口
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout = 0.5)
for j in range(10):
    if ser.isOpen() == False:
        ser.open()
    #### 每次循环对上值次取反
    if i == 0:
       i = 1
    else:
       i = 0
    #### 向串口发送字符
    ser.write(chr(i))
    #### 获取串口返回值
    #### linux为福阻塞模式,在阻塞模式下
    #### 会报错,所以抱起来就好了。
    try:
        re = ser.readlines()
    except:
        pass
    print re
    time.sleep(2)

  51单片机代码

#include 
#include 
typedef unsigned char uint8;
typedef unsigned int  uint16;


uint8 num;
sbit D0 = P0^0; 

void init()
{
	SCON = 0x50;
    TMOD = 0x20;

	TH1 = 0xFD;
	TL1 = 0xFD;
	TR1 = 1; //打开定时器
	ES  = 1;
	EA  = 1; //打开总开关
}

void interrupt_uart() interrupt 4
{
	if(TI)
	{
		TI = 0;
		REN = 1;
	}
	if(RI)
	{
		RI = 0;
		num = SBUF;	
		if (num == 1)
		{
			D0 = 0;
			SBUF = 0;
		}
		else
		{
			D0 = 1;
			SBUF =1;
		}
		REN = 0;
	}
}


main()
{
	init();
	while(1);
}

源码下载
raspberrypito51

1月 08

python 连接hbase存、取图片

  连接hbase1.0.4需要使用Thrift,我用的是python2.6。
  安装thrift。下载地址https://dist.apache.org/repos/dist/release/thrift/0.9.0/thrift-0.9.0.tar.gz解压后安装命令。
在hbase服务器上,确保hbase服务已经启动。在thrift目录中,用管理员运行一下命令安装。
./configure
make
make install

  安装完毕生成hbase的client代码命令格式如下,
thrift –gen
登陆到hbase的权限进入
$ cd hbase/src/main/resources/org/apache/hadoop/hbase/thrift

生成python的
$ thrift –gen py Hbase.thrift
再生成一个C的学习备用,与本文无关
$ thrift –gen c_glib Hbase.thrift

将gen-py文件夹下的hbase文件夹拷贝到要连接hbase的服务器的python目录下,我用的是python2.6,自己手动安装的。命令如下
cp -R hbase /usr/local/lib/python2.6/site-packages/

拷贝完毕用import导入 hbase成功。开始写代码了。参考hbase里的例子在hbase/src/examples/中。

  我的任务就是把某个目录下,以jpg结尾的图片放到hbase里,因为图片名没有重复,所以用图片名做row name。hbase手动建表’hbase(main):013:0> create ‘img’, ‘data:”。

  首先统计一下照片的数量。这个image目录下只有jpg的图片,使用匹配只是备将来使用。下面只是测试脚本,不关心业务逻辑。

# find /image/ -name \*.jpg -type f |wc -l
13140

# du -s -h /image
303M /image/

  本地共有13140张照片共303M,写入hbase测试脚本如下:

#!/bin/bash 
# -------------------------------
# Revision:
# Date:        2012-12-11 
# Author:      simonzhang 
# Email:       simon-zzm@163.com 
# Web:         www.simonzhang.net 
# -------------------------------

import os
import re

from thrift.transport import TSocket  
from thrift.transport import TTransport  
from thrift.protocol import TBinaryProtocol  
   
from hbase import Hbase  
from hbase.ttypes import *

#### base set
find_path=(r'/image/',
           )

class HbaseWrite():
    def __init__(self):
        self.tableName = 'img'
        self.transport = TSocket.TSocket('192.168.100.100', 9090)
        self.transport = TTransport.TBufferedTransport(self.transport)
        self.transport.open()
        self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
        self.client = Hbase.Client(self.protocol)

    def createTable(self, tableName):
        col1 = ColumnDescriptor(name="data:",maxVersions=1)
        self.client.createTable(tableName,[col1])

    def write(self, PicPath, PicName):
        row = PicName.split('.')[0]
        _data = PicName.split('.')[1]
        PicData = open('%s/%s' % (PicPath, PicName), 'rb').read()
        # 此处需要注意格式,网上的格式报错,少个参数报错如下
        # TypeError: mutateRow() takes exactly 5 arguments (4 given)
        self.client.mutateRow(self.tableName, row, [Mutation(column="data:%s" % _data, value=PicData)], {})

    def read(self, tableName, PicName):
        row = PicName.split('.')[0]
        data_type = PicName.split('.')[1]
        get_data = self.client.get(tableName, row, 'data:%s' % data_type, {})[0]
        if get_data:
            return get_data.value
        else:
            return "Error"


def main(_path):
    WHB = HbaseWrite()
    WHB.createTable()
    find_file=re.compile(r"^[0-9a-z]*.jpg$")
    find_walk=os.walk(_path)
    for path,dirs,files in find_walk:
        for f in files:
            if find_file.search(f):
                path_name=path
                file_name=f
                WHB.write(path_name, file_name)


if __name__ == "__main__":
    for get_path in find_path:
        main(get_path)

开始测试脚本
# time python hbase_test.py

real 1m15.471s
user 0m4.881s
sys 0m2.867s

到hbase里查看写入的数量,证明已经完全写入。
hbase(main):001:0> count ‘img’
:
:
:
13140 row(s) in 10.2780 seconds

2013-5-16. 因为对hadoop理解不足。以下写的有问题,提醒大家注意。

hbase使用hadoop进行存储,查看hadoop的磁盘使用量。
26K namenode1/
298M u01/

  我的内存给namenode可以使用25G。根据以上数据计算结果如下:
((25*1000*1000)/26)*298= 286538461M = 286538G = 286 T

  如果每台服务器有三块1T存储硬盘,此集群可以有95台服务器。共存储此类照片大约为12634615360张。内网测试,写入速度3.9M。

  注:有一点需要注意,写入的数据删除后磁盘空间也不会释放,原理应该改和mongodb一样,但是没有仔细查看。