3月 29

linux下python、go、C库处理图片缩放效率对比

  找到go语言的源码
https://github.com/nfnt/resize
http://code.google.com/p/gorilla/source/browse/lib/appengine/example/moustachio/resize/resize.go?r=3dbce6e267e9d497dffbce31220a059f02c4e99d

  使用’go get ‘安装需要使用git。如果是centos 6 直接安装’yum install git’。但是我的是CentOS 5 还要手动安装一下。
# yum install -y gcc make curl curl-devel zlib-devel openssl-devel perl perl-devel

cpio expat-devel gettext-devel
# wget http://codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
# tar zxvf git-latest.tar.gz
# cd git-2013-03-28/
# autoconf
# ./configure
# make && make install
# git –version

  git安装完成,开始测试。我的环境,python2.6,go1.0.2,ImageMagick是C/C++语言开发使用也比较广泛。直接用命令测试。。使用一张150k 510×382的图片做测试。缩成宽300的等比例缩小图。

# go get github.com/nfnt/resize

go 测试代码

package main

import (
    "github.com/nfnt/resize"
    "image/jpeg"
    "os"
)

func main() {
    // open "test.jpg"
    file, err := os.Open("test.jpg")
    if err != nil {
        print("Open File Error")
    }

    // decode jpeg into image.Image
    img, err := jpeg.Decode(file)
    if err != nil {
        print("Not image file")
    }
    file.Close()

    // resize to width 1000 using Lanczos resampling
    // and preserve aspect ratio
    m := resize.Resize(300, 0, img, resize.Lanczos3)

    out, err := os.Create("test_go.jpg")
    if err != nil {
        print("Save Image Error!")
    }
    defer out.Close()

    // write new image to file
    jpeg.Encode(out, m, nil)
}

python 测试代码

#!/bin/env python
# -*- coding:utf-8 -*-
# --------------------------------
# Filename:    cut_image.py
# Revision:    1.1
# Author:      simon-zzm
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# --------------------------------
import Image


def main():
    file = Image.open('test.jpg')
    w = file.size[0]
    h = file.size[1]
    re_data = file.resize((300, int(h/(float(w)/300))),)
    re_data.save('test_py.jpg', 'JPEG',)


if __name__ == '__main__':
    main()

  在linux下使用time进行测试结果
# time python cut_image.py

real 0m0.051s
user 0m0.040s
sys 0m0.009s

# time go run cut_image.go

real 0m2.736s
user 0m2.695s
sys 0m0.039s

# time convert -resize 300x test.jpg test_c.jpg

real 0m0.073s
user 0m0.070s
sys 0m0.002s

-rw-r–r– 1 root root 150332 Jul 16 2012 test.jpg
-rw-r–r– 1 root root 12929 Mar 28 23:13 test_go.jpg
-rw-r–r– 1 root root 13087 Mar 28 23:13 test_py.jpg
-rw-r–r– 1 root root 58591 Mar 28 23:14 test_c.jpg

总结:GO使用这个方法作为图片缩放的处理速度和python处理速度的差距太大了。烦请高手指点如何处理。不过GO的语法还是比较简单,值得学习。之前做的多语言简单累加计算测试,GO效率还是比较高,所以处理业务逻辑处理的效率还应该不错。
源码下载

9月 27

linux下用python生成二维码


【张子萌 2011-9-27】
库的官方网站

http://fukuchi.org/works/qrencode/index.en.html

需要使用CythonCython是用来生成 C 扩展到而不是独立的程序的。所有的加速都是针对一个已经存在的 Python

用的一个函数进行的。还需要安装PIL

先下载了qrencode-3.1.1.tar.gz,使用configuremakemake install进行安装,手动生成了一个图,却是可用。

开始制作。

安装Cypthon

# easy_install cython

在官方下载bitly-pyqrencode-1cfb23c.tar.gz

# tar zxvf bitly-pyqrencode-1cfb23c.tar.gz

# cd bitly-pyqrencode-1cfb23c

使用Cpython安装qrencode

# cython qrencode.pyx

阅读README确认系统中有以下文件

you need libqrencode somewhere in your LD path (/usr/local/lib)

you need qrencode.h somewhere on your include path (/usr/local/include)

# python setup.py install

安装完毕测试以下。

脚本如下

from qrencode import Encoder

cre = Encoder()

img = cre.encode(“博客地址:http://simon-zzm.blog.163.com/ n电子邮件:simon-zzm@163.com”, { ‘width’: 300 })

img.save(‘out.png’)

生成图片如下:

linux下用python生成二维码 - simon-zzm - simon个人观点

3月 25

导出mysql 上传到ftp备份

导出mysql的库上传到ftp服务器备份,上传完毕删除本地文件。用crontab定时运行。用shell也很容以实现,毕竟在学习python,练习一下。

#!/usr/bin/env python
#-*- coding: utf-8 -*-
# ——————————————————————————-
# Filename: Back mysql
# Revision: 1.0
# Date: 2010-03-24
# Author: simon-zzm
# Email: simon-zzm@163.com
# Description:
# ——————————————————————————-
import os,time,sys
from ftplib import FTP
import datetime

###################设置参数
mysql_user=’root’
mysql_pass=’123456′
mysql_comm=’/usr/local/mysql/bin/mysqldump’
mysql_data=[‘test_a’,’test_b’] ###可以填写多个库
mysql_local_path=’/usr/local/checksystem/’
ftp_ip=’x.x.x.x’
ftp_user=’ftpuser’
ftp_pass=’test’
admin_mail=’simon-zzm@163.com’
###################开始运行
#导出的文件为gz的压缩文件,命名格式为ip地址、库名、导出日期组成。
###获得当前时间
get_time_now=time.strftime(‘%Y-%m-%d’)
###获得本机IP地址
ip = os.popen(“/sbin/ifconfig | grep ‘inet addr’ | awk ‘{print $2}'”).read()
get_local_ip = ip[ip.find(‘:’)+1:ip.find(‘n’)]
for i in mysql_data:
###拼成包名
mysql_back_name=mysql_local_path+get_local_ip+’_’+i+’_’+get_time_now+’.gz’
###拼成导出命令
gzip_command = “%s -u%s -p%s –opt %s |gzip >%s” %(mysql_comm,mysql_user,mysql_pass,i,mysql_back_name)
###导出并判断是否成功
if os.system(gzip_command)==0:
print ‘Back Successful’
###导出成功上传到ftp服务器
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect(ftp_ip,’21’)
ftp.login(ftp_user,ftp_pass)
file_handler = open(mysql_back_name,’rb’)
bufsize = 1024
ftp.storbinary(‘STOR %s’ % os.path.basename(mysql_back_name),file_handler,bufsize)
ftp.set_debuglevel(0)
file_handler.close()
ftp.quit()
print ‘up load over!’
###偷个懒使用linux命令将本地文件删掉。
os.popen(“/usr/bin/find “+mysql_local_path+”* -name *.gz -exec rm -rf {} ; “)
else:
print ‘false’
###如果导出失败,给管理员发个信,偷个懒使用linux命令解决。
mail_url=”/bin/mail -s “+get_local_ip+”back fail “+admin_mail+” os.popen(mail_url)

3月 01

shell 打印拷贝进度

写了一个脚本做大量数据拷贝,但是拷贝的时间比较常,不知道当前进度如何,是否死机,于是写了一段关于拷贝百分比的输出,详细参见以下脚本。

#!/bin/bash
# ——————————————————————————-
# Filename: copy_percent.sh
# Revision: 1.0
# Date: 2011-03-01
# Author: simon-zzm
# Email: simon-zzm@163.com
# ——————————————————————————-

SOURCE=$1
TARGET=$2

/bin/cp -r “$SOURCE” “$TARGET” &

compute_percent() #
{
SOURCE_SIZE=`/usr/bin/du -s ${1}|awk ‘ ”{print $1}’`
TARGET_SIZE=`/usr/bin/du -s ${2}|awk ‘ ”{print $1}’`
let “i=(${TARGET_SIZE}*100)/${SOURCE_SIZE}”
return $i
}

compute_percent ${SOURCE} ${TARGET}
while [ ${i} -lt ‘100’ ]
do
compute_percent ${SOURCE} ${TARGET}
echo $i’%’
sleep 1
done
echo “ok”

3月 28

nagios 安装调试

[整理人:张子萌 2010-04]

需要准备软件如下:
centos

nagios-3.2.0
nagios-plugin-1.4.14

除了nagios还需要安装以下软件
apache2 安装路径为/usr/local/apache
perl
gcc 编译工作系统默认即可,如果没有安装推荐使用yum安装。
php nagios3.2页面需要php环境
yum install gcc glibc glibc-common
绘制图表需要安装以下包
freetype-2.3.5.tar.gz
libiconv-1.11.tar.gz
libmcrypt-2.5.7.tar.gz
libpng-1.2.10.tar.bz2
jpegsrc.v6b.tar.gz
gd-2.0.35.tar.gz
可以使用yum或者源码安装。
以上绘图包和apache安装可以参照:http://simon-zzm.blog.163.com/blog/static/88809522201028104721790/

1. 建组建帐号nagios

# groupadd nagios
# groupadd apache
# useradd -g nagios -G apache nagios
# useradd -g apache -G nagios apache

2. 编译安装nagios

# tar zxvf nagios-3.2.0.tar.gz
# cd nagios-3.2.0
# ./configure –prefix=/usr/local/nagios
–with-command-group=nagios
–with-httpd-conf=/usr/local/apache/conf/
# make all
# make install
# make install-init
# make install-config
# make install-commandmode
# make install-webconf

3. 装nagios-plugin-1.4.13

# tar zxvf nagios-plugin-1.4.14.tar.gz
# cd nagios-plugin-1.4.14
# ./configure –prefix=/usr/local/nagios –with-nagios-user=nagios –with-nagios-group=nagios
# make
# make install

注:centos4 上安装时
在checking for redhat spopen problem…停住。
需要添加–enable-redhat-pthread-workaround参数

4. 创建web登录nagios账号

# cd /usr/local/apache/bin
# ./htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin ;创建一个登录WEB的帐号

5. 配置nagios和apache整合
本文使用虚拟机配置。
# cd /usr/local/apache/conf/extra/
# vi httpd-vhosts.conf

配置apache的cgi ,在httpd-vhosts.conf后增加配置如下:

ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin

Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName “Nagios Access”
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user

Alias /nagios /usr/local/nagios/share

Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName “Nagios Access”
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user

首先测试配置文件是否正确
# /usr/local/apache/bin/apachectl configtest

重启apache
# /usr/local/apache/bin/apachectl stop
# /usr/local/apache/bin/apachectl start

6. 启动nagios

测试nagios配置是否可用
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

如果可用启动服务
# /etc/init.d/nagios start

现在你可以通过浏览器访问监控的界面了,http://x.x.x.x/nagios

7. 定义监控组

7.1 建立test监控组,监控192.268.1.103服务器。

在/usr/local/nagios/etc/目录下建立test目录,并定义主机.
监控192.168.1.103服务器的配置文件名为test_1_103.cfg.
# mkdir -p /usr/local/nagios/etc/test
# vi /usr/local/nagios/etc/testgroup.cfg
# 定义主机
define host{
use test-host
alias test Linux Server
address test_1_103; 如多个文件可以用逗号分开
}

7.2 建立监控文件
# vi /usr/local/nagios/etc/test_1_103.cfg
# 定义Ping远程Linux主机
define service{
use test-service ; 这个名字要和监控模板中一样,见7.3
host_name test;
service_description PING;
check_command check_ping!100.0,20%!500.0,60% ;check_ping命令在commands.cfg中定义,后跟两个参数,命令及参数间用!分割。
}
# 检查远程Linux主机根分区使用情况,必须安装nrpe并在/usr/local/nagios/etc/objects/commands.cfg中定义,见第8部分。
define service{
use generic-service ; Name of service template to use
host_name sectop
service_description Root Partition
check_command check_nrpe!check_disk_root
}
# 检查远程Linux主机的登录人数
define service{
use generic-service ; Name of service template to use
host_name sectop
service_description Current Users
check_command check_nrpe!check_users
}
# 检查远程Linux的主机的负载
define service{
use generic-service ; Name of service template to use
host_name sectop
service_description Current Load
check_command check_nrpe!check_load
}
# 检查远程Linux主机swap分区使用情况
define service{
&nb
sp; use generic-service ; Name of service template to use
host_name sectop
service_description Swap Usage
check_command check_nrpe!check_swap
}
# 检查远程Linux主机的SSH服务
define service{
use generic-service ; Name of service template to use
host_name sectop
service_description SSH
check_command check_ssh
notifications_enabled 1 ;0为不发送告警信息 1为发送

}
# 检查远程Linux主机的HTTP服务
define service{
use generic-service ; Name of service template to use
host_name sectop
service_description HTTP
check_command check_http
notifications_enabled 1 ;0为不发送告警信息 1为发送
}

7.3 定义监控组的模板
# vi /usr/local/nagios/etc/objects/templates.cfg

define contact{
name test-contact ; The name of this contact template
service_notification_period 24×7 ; service notifications can be sent anytime
host_notification_period 24×7 ; host notifications can be sent anytime
service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events
host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events
service_notification_commands notify-service-by-email ; send service notifications via email
host_notification_commands notify-host-by-email ; send host notifications via email
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}

define host{
name test-host ; The name of this host template
notifications_enabled 1 ; Host notifications are enabled
event_handler_enabled 1 ; Host event handler is enabled
flap_detection_enabled 1 ; Flap detection is enabled
failure_prediction_enabled 1 ; Failure prediction is enabled
process_perf_data 1 ; Process performance data
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; Retain non-status information across program restarts
notification_period 24×7 ; Send host notifications at any time
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL HOST, JUST A TEMPLATE!
}

define host{
name te
st-server ; The name of this host template
use test-host ; This template inherits other values from the generic-host template
check_period 24×7 ; By default, Linux hosts are checked round the clock
check_interval 5 ; Actively check the host every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute intervals
max_check_attempts 10 ; Check each Linux host 10 times (max)
check_command check-host-alive ; Default command to check Linux hosts
notification_period workhours ; Linux admins hate to be woken up, so we only notify during the day
notification_interval 120 ; Resend notifications every 2 hours
notification_options d,u,r ; Only send notifications for specific host states
contact_groups admins ; Notifications get sent to the admins by default
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL HOST, JUST A TEMPLATE!
}

define service{
name test-service ; The ‘name’ of this service template
active_checks_enabled 1 ; Active service checks are enabled
passive_checks_enabled 1 ; Passive service checks are enabled/accepted
parallelize_check 1 ; Active service checks should be parallelized (disabling this can lead to major performance problems)
obsess_over_service 1 ; We should obsess over this service (if necessary)
check_freshness 0 ; Default is to NOT check service ‘freshness’
notifications_enabled 1 ; Service notifications are enabled
event_handler_enabled 1 ; Service event handler is enabled
flap_detection_enabled 1 ; Flap detection is enabled
failure_prediction_enabled 1 ; Failure prediction is enabled
process_perf_data 1 ; Process performance data
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; Retain non-status information across program restarts
is_volatile 0 ; The service is not volatile
check_period 24×7 ; The service can be checked at any time of the day
max_check_attempts 3 ; Re-check the serv
ice up to 3 times in order to determine its final (hard) state
normal_check_interval 10 ; Check the service every 10 minutes under normal conditions
retry_check_interval 2 ; Re-check the service every two minutes until a hard state can be determined
contact_groups admins ; Notifications get sent out to everyone in the ‘admins’ group
notification_options w,u,c,r ; Send notifications about warning, unknown, critical, and recovery events
notification_interval 60 ; Re-notify about service problems every hour
notification_period 24×7 ; Notifications can be sent out at any time
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}

7.4 修改告警模板
# vi vi /usr/local/nagios/etc/objects/contact.cfg
define contact{
contact_name nagiosadmin ; Short name of user
use test-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email admin@mail.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}

define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}

7.5 添加加载项

在nagios.cfg配置文件中开启对/usr/local/nagios/etc/test/加载
cfg_dir=/usr/local/nagios/etc/test/testgroups.cfg
cfg_dir=/usr/local/nagios/etc/test/test_1_103.cfg

测试nagios配置是否可用
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

如果重启服务
# /etc/init.d/nagios restart

8. 安装nrpe
nrpe用于和远程服务器交互使用。
nrpe分为两部分:一部分是运行在client端。一部分运行在客户端

8.1 nagios服务器上安装
#tar xzvf nrpe-2.12.tar.gz
#cd nrpe-2.12
#./configure
#make all
#make install-plugin #服务器端只要安装nrpe监控插件就行

在/usr/local/nagios/etc/objects/commands.cfg中定义check_nrpe命令
#vi /usr/local/nagios/etc/objects/commands.cfg
添加如下
# ‘check_nrpe ‘ command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

8.2 在要监控的客户端上
参照 1 中在客户服务器上建立nagios用户组和nagios用户
# tar xzvf nagios-plugins-1.4.14.tar.gz
# cd nagios-plugins-1.4.14
# ./configure –prefix=/usr/local/nagios
# make
# make install
# chown -R nagios:nagios /usr/local/nagios/

# tar xzvf nrpe-2.12.tar.gz
# cd nrpe-2.12
# ./configure –prefix=/usr/local/nagios
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
修改配置文件
# vi /usr/local/nagios/etc/nrpe.cfg #修改nrpe配置文件,允许Nagios监控服务器监控本机

allowed_hosts=127.0.0.1,192.168.0.19 #此处IP为监控服务器的地址,如多个地址可以使用逗号分隔

启动客户端
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
查看端口是否已经监听
#netstat -tln #查看端口
tcp 0 0 0.0.0.0:5666 0.0.0.0:*

添加随系统启动,启动nrep客户端
#vi /etc/rc.d/rc.local
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

9. 设置被监控的windows服务器。
找了个NSClient++-windows*.msi的安装包,安装完毕修改配置,在nagios服务器也需要配
置,此处不做详解。

附录:
1. 配置nagios及其plugins

/usr/local/nagios/etc下的文件
#控制cgi访问的配置文件
cgi.cfg

#Nagios主配置文件
nagios.cfg

#resource.cfg定义了一些变量,以便被其它文件引用,如$USER1$
resource.cfg

# objects是一个目录,用于定义Nagios对象
objects

/usr/local/nagios/etc/objects下的文件

#命令