免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: yuhongchun
打印 上一主题 下一主题

专家座谈——脚本分享与交流(大奖) [复制链接]

论坛徽章:
0
71 [报告]
发表于 2011-09-17 10:58 |只看该作者
本帖最后由 crulat 于 2012-02-20 13:48 编辑

批量部署Nagios监控服务的脚本:

  1. #!/bin/bash

  2. # Check the parameters
  3. if [ $# -eq 0 ]; then
  4.    echo "please run with your file"
  5.    exit 1
  6. fi

  7. # Global variables

  8. # File is the list of ip addresses that map to hostnames for all  given servers
  9. file=$1
  10. cfg_path=/usr/local/nagios/etc

  11. # The four variables below need to be modified as your required.
  12. group_name=node
  13. host_cfg=${cfg_path}/hosts/node/node.cfg
  14. service_cfg=${cfg_path}/services/node.cfg
  15. monitor=(zombie_procs total_procs users load var swap)

  16. # The core configure files
  17. group_cfg=${cfg_path}/hostgroups.cfg
  18. nagios_cfg=${cfg_path}/nagios.cfg

  19. # Backup dir
  20. mkdir -p /root/backup
  21. BACKDIR=/root/backup

  22. # Backup the core configure files to /root/backup and define the filename by the current date
  23. [ ! -e ${group_cfg} ] || cp -a ${group_cfg} ${BACKDIR}/hostgroups.$(date +%F-%T).cfg
  24. [ ! -e ${nagios_cfg} ] || cp -a ${nagios_cfg} ${BACKDIR}/nagios.$(date +%F-%T).cfg

  25. base_host=$(basename ${host_cfg})
  26. base_service=$(basename ${service_cfg})
  27. back_host=${base_host%.cfg}
  28. back_service=${base_service%.cfg}

  29. # Backup the core configure files to /root/backup and define the filename by the current date
  30. [ ! -e ${host_cfg} ] || cp -a ${host_cfg} ${BACKDIR}/${back_host}.H.$(date +%F-%T).cfg
  31. [ ! -e ${service_cfg} ] || cp -a ${service_cfg} ${BACKDIR}/${back_service}.S.$(date +%F-%T).cfg

  32. # The host_fun function
  33. host_fun()
  34. {
  35. HOST="
  36. define host{
  37.        host_name                       ${name_host}
  38.        alias                           ${name_host}
  39.        address                         ${ip}
  40.        max_check_attempts              5
  41.        check_period                    24x7
  42.        contact_groups                  admins
  43.        notification_interval           10
  44.        notification_period             24x7
  45.        notification_options            d,u,r
  46.        }"
  47. }


  48. # The share function
  49. share()
  50. {

  51. echo "is_volatile              0
  52.     check_period             24x7
  53.     max_check_attempts       5
  54.     normal_check_interval    2
  55.     retry_check_interval     1
  56.     contact_groups           admins
  57.     notification_options     w,u,c,r
  58.     notification_interval    960
  59.     notification_period      24x7"
  60. }

  61. # The host_alive_fun function
  62. host_alive_fun()
  63. {
  64. SERVICE="

  65. define service{
  66.     use                      generic-service      
  67.     host_name                ${name_service}
  68.     service_description      check-host-alive
  69.     $(share)
  70.     check_command            check-host-alive
  71.     }
  72. "
  73. }

  74. # The check_tcp_fun function
  75. check_tcp_fun()
  76. {
  77. SERVICE="

  78. define service{
  79.     use                      generic-service     
  80.     host_name                ${name_service}
  81.     service_description      check_port80
  82.     $(share)
  83.     check_command            check_tcp!80
  84.     }
  85. "
  86. }

  87. # The service_fun function
  88. service_fun()
  89. {
  90. SERVICE="

  91. define service{
  92.     use                      generic-service      
  93.     host_name                ${name_service}
  94.     service_description      check_${1}
  95.     $(share)
  96.     check_command            check_nrpe!check_${1}
  97.     }
  98. "
  99. }

  100. # The read function
  101. WRL()
  102. {

  103. while read line
  104. do
  105.       ${1}
  106. done << EOF
  107. $(egrep -v "^#|^$" ${file})
  108. EOF

  109. }

  110. # Create ${host_cfg}
  111. CHC()
  112. {
  113.       name_host=$(echo ${line} | awk '{print $2}')
  114.       ip=$(echo ${line} | awk '{print $1}')
  115.       host_fun
  116.       echo "${HOST}" >> ${host_cfg}
  117. }

  118. # Create ${service_cfg}
  119. CSC()
  120. {
  121.       name_service=$(echo ${line} | awk '{print $2}')
  122.       for stac in host_alive_fun check_tcp_fun
  123.       do
  124.             ${stac}
  125.             echo "${SERVICE}" >> ${service_cfg}
  126.       done
  127.       for SEV in ${monitor[*]}
  128.       do
  129.             service_fun ${SEV}
  130.             echo "${SERVICE}" >> ${service_cfg}
  131.       done
  132. }

  133. WRL CHC
  134. WRL CSC

  135. # Append the hostgroups to ${group_cfg}

  136. name=$(cat ${file} | awk '{print $2}' | xargs | sed 's# #,#g')

  137. GROUP="
  138. define hostgroup {
  139.        hostgroup_name          ${group_name}_group
  140.        alias                   ${group_name}
  141.        members                 ${name}
  142.        }"

  143. echo "${GROUP}" >> ${group_cfg}

  144. # Add the code below to ${nagios_cfg}

  145. config=(host_cfg service_cfg)
  146. for cfg in ${config[*]}
  147. do
  148.      [[ x$(grep "cfg_file=${!cfg}" ${nagios_cfg}) != x ]] || echo "cfg_file=${!cfg}" >> ${nagios_cfg}
  149. done

  150. # Check the configuration
  151. # bash /root/check_nagios.sh
  152. /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

  153. # Restart the service
  154. service nagios restart
复制代码

论坛徽章:
0
72 [报告]
发表于 2011-09-17 11:07 |只看该作者
本帖最后由 crulat 于 2011-09-17 11:12 编辑

并行执行命令的脚本:

  1. #!/bin/bash
  2. #
  3. # pssh

  4. CMD=$1
  5. IP_FILE=$2
  6. DIR=/usr/local/etc/

  7. if [[ $# != 2 ]]; then
  8.     echo "Usage: pssh \"commands\" ip_file"
  9.     exit 1
  10. fi

  11. if [ ! -e ${DIR}${IP_FILE} ]; then
  12.     echo "file ${DIR}${IP_FILE} doesn't exist"
  13.     exit 1
  14. fi

  15. for IP in $(egrep -v "^#|^$" ${DIR}${IP_FILE} | sort -n | uniq)
  16. do
  17.      echo "---> ${IP}"
  18.      ssh ${IP} ${CMD} || echo ${IP} >> failed_ip_list &
  19. done 2> /dev/null
  20. wait &> /dev/null
复制代码
并行传输数据的脚本:

  1. #!/bin/bash
  2. #
  3. # ptar

  4. FILE=$1
  5. TDIR=$2
  6. IP_FILE=$3
  7. DIR=/usr/local/etc/

  8. if [[ $# != 3 ]]; then
  9.     echo "Usage: ptar \"file\" target_dir ip_file"
  10.     exit 1
  11. fi

  12. if [ ! -e ${FILE} ]; then
  13.     echo "file ${FILE} doesn't exist"
  14.     exit 1
  15. fi

  16. if [ ! -e ${DIR}${IP_FILE} ]; then
  17.     echo "file ${DIR}${IP_FILE} doesn't exist"
  18.     exit 1
  19. fi

  20. for IP in $(egrep -v "^#|^$" ${DIR}${IP_FILE} | sort -n | uniq)
  21. do
  22.      echo "---> ${IP}"
  23.      tar -jcpvf - ${FILE} | ssh  ${IP} "cd ${TDIR}; tar -jxpvf -" || echo ${IP} >> failed_ip_list &
  24. done 2> /dev/null
  25. wait &> /dev/null
复制代码

论坛徽章:
0
73 [报告]
发表于 2011-09-17 11:08 |只看该作者
本帖最后由 dongnanyk 于 2011-09-17 11:33 编辑

小弟不才只能写简单脚本
功能:找到没用过的ip,其实就是简单ping 命令,遇到禁止icmp包就没办法了,但是ping 的 -f -W 参数还是比较有用的,前者flood ,后者timeout!

  1. #!/bin/bash
  2. #Not used to find the ip
  3. #20110909

  4. #variables
  5. ip_duan=10.0.100
  6. ip_head=1
  7. ip_end=254
  8. user=`id -u`


  9. #
  10. if [ $user -ne 0 ];then
  11.     echo "must root !!"
  12.     exit 1
  13. fi


  14. for ((i=$ip_head;i<=$ip_end;i++));do
  15.     ping -f -c 3 -W 1 "$ip_duan.$i" >/dev/null
  16.     if [ $? -eq 1 ];then
  17.         echo "$ip_duan.$i"
  18.     fi
  19. done
复制代码

论坛徽章:
0
74 [报告]
发表于 2011-09-17 11:08 |只看该作者
我们的开发环境是在一台服务器上。程序员映射目录到自己的机器上。每个人在服务器上配置一个虚拟主机。通过设置hosts来访问。每来一位新同事就得添加samba目录。添加nginx虚拟主机。svn co代码。改config.php配置等一系列操作。每位员工调动时。需要把他在服务器上的目录以及配置删掉。。下面是一个脚本来做这些事情。。包括一个模块文件
/data/software/sh/template.conf
  1. ############{{user}}############
  2.     server {
  3.         listen       80;
  4.         server_name  {user}.lava.cn;

  5.         #charset koi8-r;

  6.         access_log  logs/{user}.lava.cn.access.log;
  7.         error_log   /data/weblog/{user}.lava.cn.error.log;
  8.         location / {
  9.             root   /home/{user}/gamebox/htdocs;
  10.             index  index.php index.html index.htm;
  11.         }


  12.         # pass the PHP scripts to FastCGI server listening on 192.168.1.123:9000
  13.         #
  14.         location ~ \.php$ {
  15.             root           /home/{user}/gamebox/htdocs;
  16.             fastcgi_pass   127.0.0.1:9000;
  17.             fastcgi_index  index.php;
  18.             fastcgi_param  FILE_SCRIPTNAME  $document_root$fastcgi_script_name;
  19.             fastcgi_param  PHP_VALUE "include_path=/home/{user}/gamebox/include:/usr/share/pear";
  20.             include        fastcgi_params;
  21.         }
  22.     include vhosts/rewrite.conf;
  23.     }
  24. ############{{user}}############
  25. ##########**{{user}}**##########
  26. [{user}]
  27. comment = {user}.lava.cn
  28. path = /home/{user}
  29. public = no
  30. browseable=no
  31. writable = yes
  32. printable = no
  33. valid users = {user}
  34. write list = {user}
  35. read only = no
  36. create mask = 775
  37. delete readonly = yes
  38. ##########**{{user}}**##########
复制代码
  1. #!/bin/bash
  2. #filename:adduser.sh
  3. #add user script
  4. #1. add nginx conf
  5. #2. add samba conf
  6. #3. add system user
  7. #4. add samba user
  8. #5. reload nginx conf
  9. #6. reload samba conf
  10. #7. mkdir smarty tmp directory

  11. TEMPLATE='/data/software/sh/template.conf'
  12. SAMBACONF='/etc/samba/smb.conf'
  13. SMBPASSWD='/usr/bin/smbpasswd'
  14. SMBSBIN='/etc/rc.d/samba'
  15. NGINXCONF='/etc/nginx/conf/nginx.conf'
  16. NGINXCONFDIR='/etc/nginx/conf/vhosts'
  17. NGINXSBIN='/usr/sbin/nginx'
  18. function addnginxconf
  19. {
  20.     USERNGINXCONF=${NGINXCONFDIR}/${1}.lava.cn.conf
  21.     read  num1 num2 <<< `grep -n "##{$1}##" $NGINXCONF | cut -d: -f1`
  22.     if [ -z $num1 ];then
  23.         read num1 num2 <<< `grep -n '##{{user}}##' $TEMPLATE | cut -d: -f1`
  24.         touch $USERNGINXCONF
  25.         sed -n -e "s/{user}/${1}/g" -e "$num1,${num2}p" $TEMPLATE > $USERNGINXCONF
  26.         read last <<< `grep -n '}' $NGINXCONF | grep -v '#' | tail -n 1 | cut -d: -f1`
  27.         str="############{$1}###########\ninclude $USERNGINXCONF\;\n############{$1}############"
  28.         cp $NGINXCONF ${NGINXCONF}_add_${1}.bak
  29.         sed -i "${last}i\ ${str}" $NGINXCONF
  30.         echo "nginxconfig:add file $USERNGINXCONF and modify $NGINXCONF file"
  31.     else
  32.         echo "nginx main conf is exist $1 record , nothing to do"
  33.     fi
  34. }
  35. function delnginxconf
  36. {
  37.     USERNGINXCONF=${NGINXCONFDIR}/${1}.lava.cn.conf
  38.     read  num1 num2 <<< `grep -n "##{$1}##" $NGINXCONF | cut -d: -f1`
  39.     if [ ! -z $num1 ];then
  40.         cp $NGINXCONF ${NGINXCONF}_del_${1}.bak
  41.         sed -i "${num1},${num2}d" $NGINXCONF
  42.         rm -rf $USERNGINXCONF
  43.         echo "delelte file $USERNGINXCONF and modify nginx main file sucess"
  44.     else
  45.         echo "nginx main conf is not exist $1 record , nothing to do"
  46.     fi
  47. }
  48. function addsambaconf
  49. {
  50.     read num1 num2 <<< `grep -n "\*\*{$1}\*\*" $SAMBACONF | cut -d: -f1`
  51.     if [ -z $num1 ];then
  52.         read num1 num2 <<< `grep -n '\*\*{{user}}\*\*' $TEMPLATE|cut -d: -f1`
  53.         sed -n -e "s/{user}/$1/g" -e "$num1,${num2}p" $TEMPLATE >> $SAMBACONF
  54.         cp $SAMBACONF ${SAMBACONF}_add_${1}.bak
  55.         echo "backup samba config file in ${SAMBACONF}_add_${1}.bak modify $SAMBACONF file add user $1 sucess"
  56.     else
  57.         echo "$1 user's record is exist in $SAMBACONF , nothing to do"
  58.     fi
  59. }
  60. function delsambaconf
  61. {
  62.     read num1 num2 <<< `grep -n "\*\*{$1}\*\*" $SAMBACONF | cut -d: -f1`
  63.     if [ ! -z $num1 ];then
  64.         cp $SAMBACONF ${SAMBACONF}_del_${1}.bak
  65.         sed -i "${num1},${num2}d" $SAMBACONF
  66.         echo "modify samba config file sucess and drop user $1"
  67.     else
  68.         echo "samba conf is not exist $1 record , nothing to do"
  69.     fi
  70. }
  71. function addsystemuser
  72. {
  73.    groupadd $1
  74.    useradd -g $1 -m -s /bin/bash -d /home/${1}/ $1
  75.    chmod +x /home/${1}
  76.    echo -e "${1}\n${1}" | passwd $1
  77.    echo "add system user $1 sucess"
  78. }
  79. function delsystemuser
  80. {
  81.     userdel -r -f $1 > /dev/null 2>&1
  82.     echo "delete system user $1 sucess"
  83. }
  84. function addsambauser
  85. {
  86.     echo -e "$1\n$1" | $SMBPASSWD -s -a $1
  87.     echo "add samba user $1 sucess"
  88. }
  89. function delsambauser
  90. {
  91.     $SMBPASSWD -x $1 > /dev/null 2>&1
  92.     echo "delete samba user $1 sucess"
  93. }
  94. function co
  95. {
  96.     svn co --username ${1} --password ${1}123 svn://192.168.1.200/yanfa3/lava.cn/dev /home/${1}/gamebox
  97. }
  98. function modifyconfig
  99. {
  100.     cp /home/${1}/gamebox/include/Config.local.php /home/${1}/gamebox/include/Config.php
  101.     sed -i "3s/b/${1}/g" /home/${1}/gamebox/include/Config.php
  102. }
  103. function useage
  104. {
  105.     echo -e "useage:$0 -a/-d username"
  106. }
  107. function addu
  108. {
  109.     addnginxconf $1
  110.     addsambaconf $1
  111.     addsystemuser $1
  112.     addsambauser $1
  113.     mkdir -p /tmp/smarty/${1}/compile
  114.     chmod -R 777 /tmp/smarty/${1}/
  115.     $NGINXSBIN -t
  116.     if [ $? -eq 0 ];then
  117.          $NGINXSBIN -s reload
  118.     else
  119.          echo "nginx test parm fail , will not nginx -s reload"
  120.     fi
  121.     $SMBSBIN restart
  122.     co $1
  123.     modifyconfig $1
  124.     touch /home/${1}/gamebox/htodcs/index.html
  125.     chmod 777 /home/${1}/gamebox/htdocs/index.html
  126.     chown -R ${1}.${1} /home/${1}/
  127. }
  128. function delu {
  129.     delnginxconf $1
  130.     delsambaconf $1
  131.     delsambauser $1
  132.     delsystemuser $1
  133.     rm -rf /tmp/smarty/${1}/
  134.     $NGINXSBIN -s reload
  135.     $SMBSBIN restart
  136. }
  137. #addnginxconf shijingbo
  138. #addsambaconf $1
  139. #delnginxconf shijingbo
  140. #delsambaconf $1
  141. #addsystemuser $1
  142. #delsystemuser $1
  143. #getopts ":a:d:" opt

  144. if [ $# -ne 2 ];then
  145.     useage;exit 1;
  146. fi
  147. #addu $2

  148. while getopts ":a:d:" opt
  149. do
  150.     case $opt in
  151.         a) addu $OPTARG;;
  152.         d) delu $OPTARG;;
  153.         ?) useage;exit 1;;
  154.     esac
  155. done
  156. #$SMBSBIN restart
复制代码

论坛徽章:
0
75 [报告]
发表于 2011-09-17 11:27 |只看该作者

我是标题

本帖最后由 hackers365 于 2011-09-17 11:37 编辑

新接手一台服务器。想快速了解服务器信息。肿么办?。。发个脚本。愣说我有非法内容。。所以放在附件里了。。

collect.rar

849 Bytes, 下载次数: 154

论坛徽章:
0
76 [报告]
发表于 2011-09-17 11:41 |只看该作者
监控进程。如果down掉。。把它启动。并邮件报警。报警超过三次。则不再发送邮件报警。进程恢复以后发送恢复邮件通知。。需要安装msmtp才可以发邮件。
  1. #!/bin/bash
  2. #monitor services,if service down mail to administrator
  3. #and restart the service
  4. #services list,you can add the monitor service to it

  5. #define monitor services
  6. servicelist=(httpd nginx phpfpm)
  7. LOG_PATH=/var/log/monitor.log
  8. #define service start parameters
  9. httpd="service httpds start"
  10. httpd_log="/var/log/messages"
  11. httpd_pid="/etc/httpd/run/httpd.pid"
  12. nginx="/usr/local/nginx/sbin/nginx"
  13. nginx_log="/usr/local/nginx/logs/error.log"
  14. nginx_pid="/usr/local/nginx/logs/nginx.pid"
  15. phpfpm="/usr/local/php5.3.25/sbin/php-fpm"
  16. phpfpm_log="/usr/local/php5.3.25/var/log/php-fpm.log"
  17. phpfpm_pid="/usr/local/php5.3.25/var/run/php-fpm.pid"

  18. function ser_restart()
  19. {
  20.         if [ $# -ne 1 ];then
  21.                 :
  22.         else
  23.                 count=1
  24.                 while [ $count -lt 4 ];do
  25.                         `$1>/dev/null 2>&1`
  26.                         if [ $? -eq 0 ];then
  27.                                 return $count;
  28.                         fi
  29.                         let "count=$count+1"
  30.                 done

  31.         fi
  32.         if [ $count -eq 4 ];then
  33.                 count=0
  34.         fi
  35.         #return restart count
  36.         return $count;
  37. }
  38. function recovery_alert()
  39. {
  40.         ipaddr=`ifconfig eth0|grep 'inet addr'|awk -F: '{print $2}'|awk '{print $1}'`
  41.         subj="`date` $1 now is ok!"
  42.         log_error "$subj"
  43.         echo "$subj"|/usr/bin/mutt -s "$subj" 13876005014@139.com
  44. }
  45. function alert_admin()
  46. {
  47.         ipaddr=`ifconfig eth0|grep 'inet addr'|awk -F: '{print $2}'|awk '{print $1}'`
  48.         eval log_path=\${${1}_log}
  49.         if [ $2 -eq 0 ];then
  50.                 subj="$ipaddr: $1 is down and restart 3 counts but fail"
  51.         else
  52.                 subj="$ipaddr: $1 is down and restart $2 counts, now the service is ok!"
  53.         fi
  54.         tail -n10 $log_path>/tmp/temp
  55.         log="`date` $1 is down"
  56.         log_error "$log"
  57.         /usr/bin/mutt -s "$subj" 13876005014@139.com</tmp/temp
  58.         rm -rf /tmp/temp
  59. }
  60. function check_status()
  61. {
  62.         eval pid=\${${1}_pid}
  63.         #whether file is exist
  64.         if [ -e $pid ];then
  65.                 ps aux|grep -v 'grep'|grep "`cat $pid`" > /dev/null 2>&1
  66.                 if [ $? -eq 0 ];then
  67.                         :
  68.                 else
  69.                         return 1
  70.                 fi
  71.         else
  72.                 return 1
  73.         fi
  74. }
  75. function log_error()
  76. {
  77.         echo "$1" >> $LOG_PATH
  78. }
  79. #now,we check the service start string is OK
  80. while :
  81. do
  82.         for a in ${servicelist[@]}
  83.         do
  84.                 eval tep=\${${a}_count:=0}
  85.                 check_status $a
  86.                 if [ $? -eq 0 ];then
  87.                         #decide whether state is recovery,if recovery then send recovery alert mail
  88.                         if [ $tep -eq 3 ];then
  89.                                 recovery_alert $a
  90.                         fi
  91.                         eval ${a}_count=0
  92.                 else
  93.                         #if alert count > 3 ,then ignore alert
  94.                         eval tep=\${${a}_count:=0}

  95.                         if [  $tep -lt 3 ];then
  96.                                 #restart service
  97.                                 eval ha="\${${a}}"
  98.                                 ser_restart "$ha"
  99.                                 #alert administrator
  100.                                 alert_admin "$a" "$?"
  101.                                 let ${a}_count=${a}_count+1
  102.                         else
  103.                                 :
  104.                         fi
  105.                 fi
  106.         done
  107. sleep 10
  108. done
复制代码

论坛徽章:
6
丑牛
日期:2013-09-17 00:18:40未羊
日期:2013-10-31 12:10:47午马
日期:2013-12-07 01:58:50水瓶座
日期:2013-12-24 22:43:12水瓶座
日期:2014-03-15 21:12:13操作系统版块每日发帖之星
日期:2016-08-07 06:20:00
77 [报告]
发表于 2011-09-17 13:36 |只看该作者
我们的开发环境是在一台服务器上。程序员映射目录到自己的机器上。每个人在服务器上配置一个虚拟主机。通过 ...
hackers365 发表于 2011-09-17 11:08



这脚本相当实用,手动添加确实是很烦燥的事情,谢谢分享,另外,麻烦你告知你的ID号和想要的书,我尽量帮你申请!!

论坛徽章:
0
78 [报告]
发表于 2011-09-17 14:38 |只看该作者
本帖最后由 coralzd 于 2011-09-17 15:10 编辑

linux 负载报警脚本,系统负载超过一定数值,就重启php-cgi,并发送报警短信。
  1. #!/bin/sh
  2. #description:system load average
  3. #author:coralzd powered by www.freebsdsystem.org
  4. host=$(hostname)
  5. channel=$(hostname | sed 's/[0-9]//g')
  6. runday=$(date "+%Y-%m-%d")
  7. IPhost=$(/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
  8. i=10
  9.   while [ 1 ];do
  10. rundaytime=$(date "+%Y-%m-%d %H:%M:%S")
  11. L=$(cat /proc/loadavg|cut -c1-4)
  12. RESULT=$(echo "$L > $i"|bc)
  13. if [ "${RESULT}" == "$i" ]; then
  14. echo "$runday" >> /var/tmp/${host}.txt
  15. wget http://10.10.10.10/phpsms/smsu.php?phone=15012345678\&msg=warning%20${IPhost}%20Load%20avarage%20is%20high -O /dev/null >/dev/null 2>&1
  16. /usr/local/php52/sbin/php-fpm restart
  17. echo "${runday}" >> /var/log/${host}.txt
  18. echo "The system load average is 10+ ,php-fpm already restart" >> /var/log/${host}.txt
  19. fi
  20. sleep 600
  21. done
复制代码

论坛徽章:
0
79 [报告]
发表于 2011-09-17 14:40 |只看该作者
本帖最后由 coralzd 于 2011-09-17 15:10 编辑

文件防篡改脚本,已有文件被修改,立即发送报警短信。
  1. #!/bin/bash
  2. #description: check files shell
  3. #author:coralzd powered by www.freebsdsystem.org
  4. checkdir=/data/www/bbs.xxx.com

  5. ipadd=`ifconfig |grep "inet" |cut -c 0-36|sed -e 's/[a-zA-Z: ]//g' |grep -v "127.0.0.1"`

  6. while [ 1 ]
  7. do
  8.   DATE=`date +%Y-%m-%d.%H:%M:%S`
  9.   find ${checkdir} \( -path ${checkdir}/forumdata/threadcaches -o -path  ${checkdir}/forumdata_1/threadcaches -o -path  ${checkdir}/forumdata_1/templates  -o -path ${checkdir}/f
  10. orumdata_1/cache -o -path ${checkdir}/forumdata/dzwxuser -o -path ${checkdir}/attachments -o -path ${checkdir}/forumdata/cache -o -path ${checkdir}/forumdata/templates -o -path
  11. ${checkdir}/forumdata/dzwxuser -o -path ${checkdir}/dzwxuserid/cache -o -path ${checkdir}/forumdata_1 \) -prune -o -name "*php" -mmin -1 -print >/tmp/tmpdd
  12.   SZ=`ls -la /tmp/tmpdd|awk '{print $5}'`
  13.   if [ "${SZ}" -gt "2" ]; then
  14.         SN=`cat /tmp/tmpdd`
  15.     echo ${DATE} ${SN} >>/var/tmp/checkfile.log
  16.     wget http://10.10.10.10/phpsms/smsu.php?phone=15012345678\&msg=%E7%95%99%E6%84%8F%EF%BC%9A${ipadd}_%E5%8F%AF%E8%83%BD%E5%87%BA%E7%8E%B0%E6%96%87%E4%BB%B6%E7%AF%A
  17. 1%E6%94%B9 -O /dev/null >/dev/null 2>&1
  18.   fi
  19.   sleep 60
  20. done
复制代码

论坛徽章:
0
80 [报告]
发表于 2011-09-17 14:59 |只看该作者
nginx_php 自动安装脚本第二版。
  1. #!/bin/bash
  2. # author:coralzd powered by www.freebsdsystem.org
  3. # written by coralzd 2010.11.05
  4. # version 0.1.4 build 20110831
  5. # description: nginx php mysql install shell

  6. nginx_dir="/usr/local/nginx"
  7. php52_dir="/usr/local/php52"
  8. mysql_dir="/usr/local/mysql"

  9. function init()
  10. {

  11. yum -y install wget gcc gcc-c++ autoconf bison flex re2c  libmhash libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

  12. read -p "Now,will download nginxphp software...Y|y:"  nginxphp

  13. case "$nginxphp" in

  14.      Y|y)

  15. echo -n "starting download nginx_php ..."
  16. cat > list << "EOF" &&
  17. nginx-1.0.1.tar.gz
  18. php-5.2.17.tar.gz
  19. php-5.2.17-fpm-0.5.14.diff.gz
  20. libiconv-1.13.1.tar.gz
  21. libmcrypt-2.5.8.tar.gz
  22. mcrypt-2.6.8.tar.gz
  23. memcache-2.2.5.tgz
  24. mhash-0.9.9.9.tar.gz
  25. mysql-5.1.58.tar.gz
  26. suhosin-patch-5.2.17-0.9.7.patch
  27. pcre-8.10.tar.gz
  28. autoconf-2.13.tar.gz
  29. eaccelerator-0.9.5.3.tar.bz2
  30. PDO_MYSQL-1.0.2.tgz
  31. libunwind-0.99.tar.gz
  32. ImageMagick.tar.gz
  33. imagick-2.3.0.tgz
  34. google-perftools-1.6.tar.gz
  35. fcgi.conf
  36. php.ini
  37. my.cnf
  38. nginx.conf
  39. php-fpm.conf
  40. EOF
  41. mkdir packages
  42.         for i in `cat list`
  43. do
  44. if [ -s packages/$i ]; then
  45.   echo "$i [found]"
  46. else
  47.   echo "Error: $i not found!!!download now......"
  48.   wget http://www.freebsdsystem.org/linux/nginx-php/$i -P packages/
  49. fi
  50. wget http://www.freebsdsystem.org/linux/nginx-php/nginxd
  51. wget http://www.freebsdsystem.org/linux/nginx-php/php-fpm
  52. mv nginxd /etc/init.d/
  53. mv php-fpm /etc/init.d/
  54. chmod 755 /etc/init.d/*
  55. chkconfig --add nginxd
  56. chkconfig --add php-fpm
  57. echo "create the eaccelerator directory to /data0/cache"
  58. mkdir /data0/cache
  59. echo "create the mysql data directory to /data0/mysql/data"
  60. mkdir -p /data0/mysql/data
  61. echo "PATH=/usr/local/mysql/bin/:$PATH" >> /etc/profile
  62. source  /etc/profile
  63. done
  64. ;;

  65. *)

  66.   echo -n "exit install script"
  67.    exit 0
  68. ;;

  69. esac  

  70. groupadd www &&  useradd www -s /sbin/nologin -g www
  71. groupadd mysql && useradd mysql -s /sbin/nologin -g mysql
  72. echo "www and mysql user && group create!"
  73. cd packages/
  74. tar zxf
  75. /bin/rm -rf list

  76. echo -e "All of installed sucussful!"


  77. }
  78. function is_version()

  79. {

  80.    
  81. if [ `uname -m` == "x86_64" ];then
  82. tar zxf libunwind-0.99.tar.gz
  83. tar zxvf libunwind-0.99.tar.gz
  84. cd libunwind-0.99/
  85. CFLAGS=-fPIC ./configure
  86. make CFLAGS=-fPIC
  87. make CFLAGS=-fPIC install
  88. cd ../
  89. else

  90.         echo "your system is 32bit ,not install libunwind lib!"
  91. fi


  92. }

  93. function ins_nginx()

  94. {

  95. cd packages/
  96. is_version
  97. tar zxf google-perftools-1.6.tar.gz
  98. cd google-perftools*
  99. ./configure
  100. make
  101. make install
  102. cd ..

  103. tar zxf pcre-8.10.tar.gz
  104. cd pcre-*
  105. ./configure
  106. make
  107. make install
  108. cd ..
  109. tar zxf nginx-1.0.1.tar.gz
  110. cd nginx-1.0.1
  111. ./configure --prefix=${nginx_dir} --user=www --group=www --with-http_stub_status_module   

  112. make && make install
  113. cd ..
  114. rm -rf /usr/local/nginx/conf/nginx.conf
  115. echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
  116. cp nginx.conf /usr/local/nginx/conf/
  117. cp fcgi.conf /usr/local/nginx/conf/
  118. echo "nginx installed sucussfully!"
  119. }

  120. function ins_mysqlserver()
  121. {

  122. cd packages/
  123. tar zxf mysql-5.1.58.tar.gz
  124. cd mysql-5.1.58
  125. CHOST="x86_64-pc-linux-gnu"
  126. CFLAGS="-march=nocona -O2 -pipe"
  127. CXXFLAGS="${CFLAGS}"
  128. ./configure --prefix=/usr/local/mysql --enable-assembler --with-server-suffix=-DZWWW --enable-thread-safe-client --enable-local-infile --enable-thread-safe-client --with-big-tables --with-charset=utf8 --with-client-ldflags=-all-static --with-collation=utf8_general_ci --with-extra-charsets=all --with-mysqld-ldflags=-all-static --with-mysqld-ldflags=-ltcmalloc  --with-mysqld-user=mysql --with-plugins=partition,myisammrg --with-pthread --with-unix-socket-path=/tmp/mysql.sock --without-ndb-debug
  129. make && make install
  130. cp support-*/mysql.server /etc/init.d/mysqld
  131. cp my.cnf /etc/
  132. chmod 744 /etc/init.d/mysqld
  133. cd /usr/local/mysql

  134. chown -R mysql:mysql .
  135. rm -rf sql-bench mysql-test
  136. mkdir -p /data0/mysql/relaylog/
  137. mkdir -p /data0/mysql/binlog/
  138. chown -R mysql.mysql /data0/mysql

  139. /usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data0/mysql/data
  140. echo "mysql server  5.1.58 installed successfully!"
  141. }

  142. function ins_mysqlclient()
  143. {

  144. cd packages/
  145. tar zxf mysql-5.1.58.tar.gz
  146. cd mysql-5.1.58
  147. CHOST="x86_64-pc-linux-gnu"
  148. CFLAGS="-march=nocona -O2 -pipe"
  149. CXXFLAGS="${CFLAGS}"
  150. ./configure "--prefix=${mysql_dir}"   "--with-mysqld-user=mysql"   "--without-debug" "--with-charset=utf8"  "--with-extra-charsets=all"  "--with-pthread" "--with-big-tables" "--enable-thread-safe-client" "--enable-assembler" "--with-readline" "--with-ssl" "--enable-local-infile"  "--without-server"

  151. make && make install
  152. cd /usr/local/mysql
  153. chown -R mysql:mysql .
  154. rm -rf sql-bench mysql-test
  155. echo "mysql client  5.1.58 installed successfully!"
  156. }
  157. function ins_php52()       
  158. {
  159. cd packages/
  160. tar zxf libiconv-1.13.1.tar.gz
  161. cd libiconv-1.13.1/
  162. ./configure --prefix=/usr/local
  163. make
  164. make install
  165. cd ../

  166. tar zxf autoconf-2.13.tar.gz
  167. cd autoconf-2.13
  168. ./configuire --prefix=/usr
  169. make && make install
  170. cd ..

  171. tar zxf libmcrypt-2.5.8.tar.gz
  172. cd libmcrypt-2.5.8/
  173. ./configure
  174. make
  175. make install
  176. /sbin/ldconfig
  177. cd libltdl/
  178. ./configure --enable-ltdl-install
  179. make
  180. make install
  181. cd ../../

  182. tar zxf mhash-0.9.9.9.tar.gz
  183. cd mhash-0.9.9.9/
  184. ./configure
  185. make
  186. make install
  187. cd ../

  188. ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
  189. ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
  190. ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
  191. ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
  192. ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
  193. ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
  194. ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
  195. ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
  196. ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
  197. ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

  198. tar zxf mcrypt-2.6.8.tar.gz
  199. cd mcrypt-2.6.8/
  200. /sbin/ldconfig
  201. ./configure
  202. make
  203. make install
  204. cd ../


  205. tar zxf php-5.2.17.tar.gz
  206. gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
  207. cd php-5.2.17/
  208. patch -p1 -i ../suhosin-patch-5.2.17-0.9.7.patch
  209. ./buildconf --force  
  210. ./configure --prefix=${php52_dir} --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=${php52_dir}/etc  --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --enable-suhosin --enable-ftp
  211. make ZEND_EXTRA_LIBS='-liconv'
  212. make install
  213. cd ..
  214. cp php.ini /usr/local/php52/etc/
  215. cp php-fpm.conf /usr/local/php52/etc/

  216. echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf.d/mysql_lib.conf
  217. /sbin/ldconfig
  218. echo "php52 installed successfully!"
  219. }

  220. function ins_php52ext()

  221. {
  222. cd packages/

  223. tar zxf memcache-2.2.5.tgz
  224. cd memcache-2.2.5/
  225. ${php52_dir}/bin/phpize
  226. ./configure --with-php-config=${php52_dir}/bin/php-config
  227. make
  228. make install
  229. cd ../

  230. tar jxf eaccelerator-0.9.5.3.tar.bz2
  231. cd eaccelerator-0.9.5.3
  232. ${php52_dir}/bin/phpize
  233. ./configure --enable-eaccelerator=shared --with-eaccelerator-shared-memory --with-php-config=${php52_dir}/bin/php-config
  234. make
  235. make install
  236. cd ../

  237. #tar zxf PDO_MYSQL-1.0.2.tgz
  238. #cd PDO_MYSQL-1.0.2/
  239. #${php52_dir}/bin/phpize
  240. #./configure --with-php-config=${php52_dir}/bin/php-config --with-pdo-mysql=${mysql_dir}
  241. #make
  242. #make install
  243. #cd ../

  244. tar zxf ImageMagick.tar.gz
  245. cd ImageMagick-6.5.1-2/
  246. ./configure
  247. make
  248. make install
  249. cd ../

  250. tar zxf imagick-2.3.0.tgz
  251. cd imagick-2.3.0/
  252. ${php52_dir}/bin/phpize
  253. ./configure --with-php-config=${php52_dir}/bin/php-config
  254. make
  255. make install
  256. cd ..


  257. echo "php52 extension installed successfully!"


  258. }

  259. case $1 in


  260. init)

  261.         init
  262.          ;;
  263. ins_mysqlserver)
  264.          
  265.           ins_mysqlserver

  266.           ;;
  267. ins_mysqlclient)
  268.           ins_mysqlclient
  269.           ;;
  270. ins_nginx)
  271.            ins_nginx
  272.            ;;
  273. ins_php52)
  274.        ins_php52  
  275.            ;;
  276. ins_php52ext)
  277.        ins_php52ext
  278.        ;;
  279. *)

  280.      echo "Usage:`basename $0` {init|ins_mysqlserver|ins_mysqlclient|ins_php52|ins_php52ext}"
  281.          ;;
  282. esac
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP