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月 21

Nginx服务使用的脚本

[编写人:张子萌 2010]

Nginx服务器时使用的脚本.

1.在nginx日志出现错误后发送告警邮件,使用该脚本会在nginx的sbin目录下产生last_error_log,错误文件格式

为nginx默认

#!/bin/sh
##############################################
# AUTHOR: simon-zzm
# back log
# Ver : 1.0 For Production
# description:
# 2010-03-12 simon-zzm@163.com
##############################################
######set ip Email base path
IP=172.2.2.2
admin_mail=simon-zzm@163.com
base_path=/usr/local/nginx

###run
cut_error_log=`/bin/cat ${base_path}/sbin/last_error_log`
last_error_log=`/usr/bin/tail -1 ${base_path}/logs/error.log|/bin/cut -c 1-19`
if [ “$cut_error_log” != “$last_error_log” ];then
/usr/bin/tail -5 ${base_path}/logs/error.log>$base_path/sbin/mail1.txt
/bin/mail -s $IP_nginx_error_log $admin_mail<$base_path/sbin/mail1.txt
/bin/rm -rf $base_path/sbin/mail1.txt
echo $last_error_log>${base_path}/sbin/last_error_log
fi

2.nginx日志切割,每个小时切割一个日志,

#!/bin/sh

##############################################

# AUTHOR: simon-zzm

# back log

# Ver : 1.0 For Production

# description:

# 2010-03-12 simon-zzm@163.com

##############################################

###set base path、use every an hour to cut log
log_dir=”/usr/local/nginx/logs”
date_dir=`date +%Y%m%d%H`

###run
/bin/mv ${log_dir}/access.log ${log_dir}/access_${date_dir}.log
/bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

3.日志备份部分参照通用性日志备份

http://simon-zzm.blog.163.com/blog/static/88809522201021523828112/

3月 10

linux 下用 cronolog 切割 apache日誌

cronolog安装可见http://simon-zzm.blog.163.com/blog/static/88809522201022112040647/

apache安装位置:/usr/local/apache/

编辑配置文件httpd.conf

LogFormat “%v %h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” mylog
LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” combined
LogFormat “%h %l %u %t “%r” %>s %b” common


LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i” %I %O” combinedio

CustomLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log” combined

如果使用虚拟机可以使用以下配置

httpd-vhosts.conf

ErrorLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log”
CustomLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log” common

3月 02

linux 下用 cronolog 切割 tomcat catalina日誌

http://cronolog.org/下载cronolog软件。

cronolog-1.6.2.tar.gz

上传到服务器,操作如下:

# tar zxvf cronolog-1.6.2.tar.gz

# cd cronolog-1.6.2

# ./configure

# make && make install

如果安裝正常,cronolog安装完毕。开始修改tomcat。

编辑bin/catalina.sh文件。

首先找到“touch “$CATALINA_BASE”/logs/catalina.out”并注释掉,并编辑如下

elif [ “$1” = “start” ] ; then

shift
#touch “$CATALINA_BASE”/logs/catalina.out
if [ “$1” = “-security” ] ; then
echo “Using Security Manager”
shift
“$_RUNJAVA” “$LOGGING_CONFIG” $JAVA_OPTS $CATALINA_OPTS
-Djava.endorsed.dirs=”$JAVA_ENDORSED_DIRS” -classpath “$CLASSPATH”
-Djava.security.manager
-Djava.security.policy==”$CATALINA_BASE”/conf/catalina.policy
-Dcatalina.base=”$CATALINA_BASE”
-Dcatalina.home=”$CATALINA_HOME”
-Djava.io.tmpdir=”$CATALINA_TMPDIR”
org.apache.catalina.startup.Bootstrap “$@” start 2>&1
| /usr/local/sbin/cronolog “$CATALINA_BASE”/logs/catalina.%Y-%m-%d.out >>/dev/null &
# org.apache.catalina.startup.Bootstrap “$@” start
# >> “$CATALINA_BASE”/logs/catalina.out 2>&1 &

if [ ! -z “$CATALINA_PID” ]; then
echo $! > $CATALINA_PID
fi
else
“$_RUNJAVA” “$LOGGING_CONFIG” $JAVA_OPTS $CATALINA_OPTS
-Djava.endorsed.dirs=”$JAVA_ENDORSED_DIRS” -classpath “$CLASSPATH”
-Dcatalina.base=”$CATALINA_BASE”
-Dcatalina.home=”$CATALINA_HOME”
-Djava.io.tmpdir=”$CATALINA_TMPDIR”
org.apache.catalina.startup.Bootstrap “$@” start 2>&1
| /usr/local/sbin/cronolog “$CATALINA_BASE”/logs/catalina.%Y-%m-%d.out >>/dev/null &
# org.apache.catalina.startup.Bootstrap “$@” start
# >> “$CATALINA_BASE”/logs/catalina.out 2>&1 &

if [ ! -z “$CATALINA_PID” ]; then
echo $! > $CATALINA_PID
fi
fi

重新启动tomcat,日志分割格式如下:

catalina.yyyy-mm-dd.out

cronolog 安装后所在位置,系统版本不同可能位置不一样,以 which 或whereis命令查看到的位置为准。