轮询检测Apache状态并启用钉钉报警◆

#!/bin/bash

shell_user="root"
shell_domain="apache" shell_list="/root/ip_list"
shell_row=`cat $shell_list |wc -l` function trans_text(){
text=$1 curl 'https://oapi.dingtalk.com/robot/send?access_token=b4fcf5862088a1bc7f2bf66a' -H'Content-Type: application/json' -d'{      #指定钉钉机器人hook地址
"msgtype": "text",
"text": {
"content": "'"$text"'"
},
}'
} function apache_check_80(){
ip=$1
URL="http://$ip/index.html"
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"` if [ $HTTP_CODE != 200 ]
then
trans_text "
=================================================================
\n $ip Apache 服务器状态异常,网页返回码: '"$HTTP_CODE"' 请及时处理 ! \n
================================================================= \n"
fi
} while true
do shell_list="/root/ip_list"
shell_row=`cat $shell_list |wc -l`
for temp in `seq 1 $shell_row`
do
Ip_Addr=`cat $shell_list |head -n $temp |tail -n 1`
apache_check_80 $Ip_Addr
done sleep 10
done

一台监控主机,一台被监控主机。被监控主机分区使用率大于80%,就发告警邮件。放到crontab里面,每10分钟执行一次。

#!/bin/bash

FSMAX="80"
remote_user='root'
remote_ip=(IP地址列表)
ip_num='0' while [ "$ip_num" -le "$(expr ${#remote_ip[@]} -l)"]
do
read_num='1'
ssh "$remote_user"@"${remote_ip[$ip_num]}" df -h > /tmp/diskcheck_tmp
grep '^/dev/*' /tmp/diskcheck_tmp | awk '{print $5}'|sed 's/\%//g' > /tmp/diskcheck_num_tmp while [ "$read_num" -le $(wc -l < /tmp/diskcheck_num_tmp) ]
do
size=$(sed -n "$read_num" 'p' /tmp/diskcheck_num_tmp)
if [ "size" -gt "$FSMAX" ]
then
$(grep '^/dev/*' /tmp/diskcheck_tmp |sed -n $read_num'p' > /tmp/disk_check_mail)
$(echo ${remote_ip[$ip_num]}) >> /tmp/disk_check_mail)
$(mail -s "diskcheck_alert" admin < /tmp/disk_check_mail)
fi read_num=$(expr $read_num + 1)
done ip_num=$(expr $ip_num + 1)
done

监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告

#!/bin/bash
#monitor available disk space
#提取本服务器的IP地址信息
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "`
SPACE=` df -hP | awk '{print int($5)}'`
if [ $SPACE -ge 90 ]
then
echo "$IP 服务器 磁盘空间 使用率已经超过90%,请及时处理。"|mail -s "$IP 服务器硬盘告警" fty89@163.com
fi

实现FTP自动上传

#! /bin/bash

ftp -n << END_FTP
open 192.168.1.22
user test testing //用户名test 密码:testing
binary
prompt off //关闭提示
mput files //上传files文件
close
bye
END_FTP

mysqlbak.sh备份数据库目录脚本

#!/bin/bash

DAY=`date +%Y%m%d`
SIZE=`du -sh /var/lib/mysql`
echo "Date: $DAY" >> /tmp/dbinfo.txt
echo "Data Size: $SIZE" >> /tmp/dbinfo.txt
cd /opt/dbbak &> /dev/null || mkdir /opt/dbbak
tar zcf /opt/dbbak/mysqlbak-${DAY}.tar.gz /var/lib/mysql /tmp/dbinfo.txt &> /dev/null
rm -f /tmp/dbinfo.txt crontab-e
55 23 */3 * * /opt/dbbak/dbbak.sh

打印彩虹

declare -a ary

for i in `seq 40 49`
do ary[$i]=" "
echo -en "\e[$i;5m ${ary[@]}\e[;0m" done declare -a ary
for s in `seq 1 10000`
do
for i in `seq 40 49`
do
ary[$i]=" "
echo -en "\e[$i;5m ${ary[@]}\e[;0m"
done
done

打印菱形

#!/bin/bash

for (( i = 1; i < 12; i++))
do
if [[ $i -le 6 ]]
then
for ((j = $((12-i)); j > i; j--))
do
echo -n " "
done for ((m = 1; m <= $((2*i-1)); m++))
do
echo -n "* "
done
echo ""
#*****************************************************************************
elif [[ $i -gt 6 ]]
then
n=$((12-i))
for ((j = $((12-n)); j > n; j--))
do
echo -n " "
done for ((m = 1; m <= $((2*n-1)); m++))
do
echo -n "* "
done
echo ""
fi done

expect实现远程登陆自动交互

#!/usr/bin/expect -f

set ipaddress [lindex $argv 0]

set passwd [lindex $argv 1]

set timeout 30

spawn ssh-copy-id root@$ipaddress

expect {

"yes/no" { send "yes\r";exp_continue }

"password:" { send "$passwd\r" }

}

#expect "*from*"

#send "mkdir -p ./tmp/testfile\r"

#send "exit\r"

#expect "#" #i# 命令运行完, 你要期待一个结果, 结果就是返回shell提示符了(是# 或者$)

http心跳检测

URL="http://192.168.22.191/index.html"

THHP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`

if [ $HTTP_CODE != 200 ]
then
echo -e "apache code:"$HTTP_CODE""
fi

PV过量自动实现防火墙封IP

#!/bin/bash

log=/tmp/tmp.log

[ -f $log ] || touch $log

function add_iptales()
{
while read line
do
ip=`echo $line |awk '{print $2}'`
count=`echo $line |awk '{print $1}'`
if [ $count -gt 100 ] && [ `iptables -L -n |grep "$ip" |wc -l` -lt 1 ]
then
iptables -I INPUT -s $ip -j DROP
echo -e "$list isdropped">>/tmp/droplist.log
fi done<$log
} function main()
{
while true
do
netstat -an|grep "EST" |awk -F '[:]+' '{print $6}'|sort |uniq -c >$log
add_iptales
sleep 180
done } main

shell实现自动安装

#!/bin/bash

function MyInstall
{
if ! rpm -qa |grep -q "^$1"
then yum install $1
if [ $? -eq 0 ]
then
echo -e "$i install is ok\n"
else
echo -e "$1 install no\n"
fi
else
echo -e "yi an zhuang ! \n"
fi
} for ins in mysql php httpd
do
MyInstall $ins
done

shell实现插入排序

#!/bin/bash

declare -a array

for i in `seq 1 10`
do
array[$i]=$RANDOM done echo -e "Array_1: ${array[@]}" for (( x=1;x<=9;x++ ))
do
for(( y=1;y<=9;y++ ))
do
if [ ${array[$y]} -gt ${array[$y+1]} ]
then
temp=${array[$y]}
array[$y]=${array[$y+1]}
array[$y+1]=$temp
fi done done echo -e "Array_2: ${array[@]}"

bash实现动态进度条

#!/bin/bash
i=0
bar=''
index=0
arr=( "|" "/" "-" "\\" ) while [ $i -le 100 ]
do
let index=index%4
printf "[%-100s][%d%%][\e[43;46;1m%c\e[0m]\r" "$bar" "$i" "${arr[$index]}"
let i++
let index++
usleep 30000
bar+='#'
clear
done printf "\n"

根据文件内容创建账号

#!/bin/bash

for Uname in `cat /root/useradd.txt |gawk '{print $1}'`
do id $Uname &> /dev/null
if [ $? -eq 0 ]
then
echo -e "这个账号已存在!"
continue
fi
for Upasswd in `cat /root/useradd.txt |gawk '{print $2}'`
do
useradd $Uname &> /dev/null
echo "$Upasswd" |passwd --stdin $Uname &> /dev/null
if [ $? -eq 0 ]
then
echo -e "账号创建成功!"
else
echo -e "创建失败!"
fi done
done

实现红色进度条

#!/bin/bash

declare -a ary

for i in `seq 0 20`
do ary[$i]=" "
echo -en "\e[41;5m ${ary[@]}\e[;0m"
sleep 1 done

监控服务器网卡流量

#!/bin/bash

while : ; do
speedtime='date +%m"-"%d" "%k":"%M'
speedday='date +%m"-"%d'
speedrx_before='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'
speedtx_before='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'
sleep 2
speedrx_after='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'
speedtx_after='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'
speedrx_result=$[(speedrx_after-speedrx_before)/256]
speedtx_result=$[(speedtx_after-speedtx_before)/256]
echo"$speedday$speedtime Now_In_Speed: "$speedrx_result"kbps Now_OUt_Speed: "$speedtx_result"kbps"
sleep 2
done

检测CPU剩余百分比

#!/bin/bash

#Inspect CPU

#Sun Jul 31 17:25:41 CST 2016

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin
export PATH TERM=linux
export TERM CpuResult=$(top -bn 1 | grep "Cpu" | awk '{print $5}' | sed 's/\..*$//g') if [[ $CpuResult < 20 ]];then
echo "CPU WARNING : $CpuResult" > /service/script/.cpu_in.txt
top -bn 1 >> /service/script./cpu_in.txt
mail -s "Inspcet CPU" wl < /service/script/.cpu_in.txt
fi

检测磁盘剩余空间

#!/bin/bash

#Insepct Harddisk , If the remaining space is more than 80%, the message is sent to the wl

#Tue Aug  2 09:45:56 CST 2016

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin

export PATH

for RemainingSpace in $(df -h | awk '{print $5}' | grep -v 'Use' | sed -e 's/[%]//g')
do
if [[ $RemainingSpace > 80 ]];then
echo -e "$RemainingSpace"
echo -e "$(df -h | grep $RemainingSpace)" > /service/script/.HarddiskWarning
mail -s "disk Warning" wl < /service/script/.HarddiskWarning
fi
done

bash-实现检测apache状态并钉钉报警

#!/bin/bash

function trans_text(){
text=$1
curl 'https://oapi.dingtalk.com/robot/send?access_token=b4fcf5862088a1bc7f2bf66aea051869e62ff5879fa0e0fddb0db9b1494781c2' -H'Content-Type: application/json' -d'
{
"msgtype": "text",
"text": {
"content": "'"$text"'"
},
}'
} function desk_check(){ dftype=$1
shell_row=`df |wc -l` for i in `seq 2 $shell_row`
do temp=(`df -h |head -n $i |tail -n 1 |awk '{print $5 "\t" $6}'`)
disk="`echo ${temp[0]} |cut -d "%" -f 1`"
name="${temp[1]}"
hostname=`hostname`
IP=`ifconfig |grep -v "127.0.0.1" |grep "inet addr:" |sed 's/^.*inet addr://g'|sed 's/ Bcas..*$//g'`
#echo -e "$disk $name"
Dat=`date "+%F %T"` if [ $disk -ge $dftype ]
then
echo "
======================== \n
>磁盘分区异常< \n
主机名: $hostname \n
IP地址: $IP \n
分区名: $name \n
使用率: $disk %\n
发生时间: $Dat \n
========================= \n"
fi
done
} function apache_check(){
url=$1
URL="http://$url/"
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"` if [ $HTTP_CODE != 200 ]
then
echo "
======================== \n
>Apache服务异常<
主机名: $hostname \n
IP地址: $IP \n
返回代码: $HTTP_CODE \n
发生时间: $Dat \n
========================= \n"
fi
} while true
do
desk_check 10
apache_check 127.0.0.1 sleep 10
done

bash实现内存检测

#!/bin/bash

#Inspect Memory : If the memory is less than 500 , then send mail to wl

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin

export PATH

MEM=$(free -m | grep "Mem" | awk '{print $4}')

if [[ MEM < 500 ]];then
echo -e "Memory Warning : Memory free $MEM" > /service/script/.MemoryWarning
mail -s "Memory Warning" wl < /service/script/.MemoryWarning
fi

剩余inode检测

#!/bin/bash

#Inspcet Inode : If the free INODE is less than 200, the message is sent to the wl

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin

export PATH

for FreeInode in $(df -i | grep -v "Filesystem" | awk '{print $4}')
do
if [[ $FreeInode < 200 ]];then
echo -e "$(df -i | grep "$FreeInode")" > /service/script/.FreeInode
mail -s "FreeInode Warning" wl < /service/script/.FreeInode
fi
done

判断哪些用户登陆了系统

#!/bin/bash

declare -i count=0

while true;do

        if who |grep -q -E "^wang"
then
echo -e "用户wang 登陆了系统\n 这是第$count 次!"
break
else
let count++
fi sleep 3
done
~ 示例:找出UID为偶数的所有用户,显示其用户名和ID号; #!/bin/bash
while read line; do
userid=$(echo $line | cut -d: -f3)
if [ $[$userid%2] -eq 0 ]; then
echo $line | cut -d: -f1,3
fi
done < /etc/passwd

批量创建账号

#!/bin/bash

sum=1

while [ $sum -le 30 ]
do
if [ $sum -le 9 ]
then
user="user_0$sum"
else
user="user_$sum"
fi useradd $user
echo "123456" |passwd --stdin $user
chage -d 0 $user
let sum=sum+1 done

批量扫面存活

#!/bin/bash
#By:lyshark #nmap 192.168.22.0/24>ip MAC=`cat ip |awk '$1 == "MAC" && $NF == "(VMware)"{print $3}'` for i in `seq 1 20` do temp=`echo ${MAC[@]} |awk '{print $i}'` IP=`cat /ip |grep -B5 $temp |grep "Nmap scan"|awk '{print $5}'` echo $IP |awk '{print $1}'
done

正则匹配IP

^[0-9]{0,2}|^1[0-9]{0,2}|^2[0-5]{0,2}

 egrep "(^[0-9]{1,2}|^1[0-9]{0,2}|^2[0-5]{0,2})\.([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})\.([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})\.([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})$"

([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})
([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})
([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})
([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2}) egrep "((25[0-5]|2[0-4][0-9]|((1[0-9]{2})|([1-9]?[0-9])))\.){3}(25[0-5]|2[0-4][0-9]|((1[0-9]{2})|([1-9]?[0-9])))" ls |egrep "((25[0-5]|2[0-4][0-9]|((1[0-9]{2})|([1-9]?[0-9])))\.){3}(25[0-5]|2[0-4][0-9]|((1[0-9]{2})|([1-9]?[0-9])$))"

正则匹配邮箱

egrep "^[0-9a-zA-Z][0-9a-zA-Z_]{1,16}[0-9a-zA-Z]\@[0-9a-zA-Z-]*([0-9a-zA-Z])?\.(com|com.cn|net|org|cn)$" rui

ls |egrep "^(([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$"

实现布片效果

#!/bin/bash

function ary_go
{
$1 $2 for (( i=0;i<=$1;i++ ))
do
for (( s=0;s<=$2;s++ ))
do
if [ $[$i%2] == 0 ]
then if [ $[$s%2] == 0 ]
then
echo -en " "
else
echo -en "\e[;44m \e[;m"
fi
else
if [ $[$s%2] == 0 ]
then
echo -en "\e[;42m \e[;m"
else
echo -en " "
fi fi done
echo done } ary_go 25 50

剔除白名单以外的用户

#!/bin/bash
w | awk 'NR>=3 {printf $1 "\t" $2 "\t" $3 "\n"}' > /tmp/who.txt
for i in $(awk '{printf $1}' /tmp/bai.txt)
do
k=$(egrep -v "$i" /tmp/who.txt | awk '{printf $2} "\n"' | awk '{printf $2 "\n"}')
for j in $k
do
pkill -9 -t "$j"
done
done

Linux Shell常用脚本整理的更多相关文章

  1. linux shell -常用脚本

    题记:来源与网络和自己工作中总结.有些脚本片段,函数经常使用. 1.判断登录用户 1.1脚本 [devtac@test_1 shell]$ vi check_user.sh #! /bin/sh ec ...

  2. Linux Shell常用shell命令

    Linux Shell常用shell命令 一.文件.目录操作命令 1.ls命令 功能:显示文件和目录的信息 ls 以默认方式显示当前目录文件列表 ls -a 显示所有文件包括隐藏文件 ls -l 显示 ...

  3. [转帖]Linux Shell常用技巧(五)

    Linux Shell常用技巧(五) https://zhuanlan.zhihu.com/p/73451771 1. 变量:在awk中变量无须定义即可使用,变量在赋值时即已经完成了定义.变量的类型可 ...

  4. [转帖]拿小本本记下的Linux Shell常用技巧(一)

    拿小本本记下的Linux Shell常用技巧(一) https://zhuanlan.zhihu.com/p/73361101 一. 特殊文件: /dev/null和/dev/tty Linux系统提 ...

  5. 【shell 大系】Linux Shell常用技巧

    在最近的日常工作中由于经常会和Linux服务器打交道,如Oracle性能优化.我们数据采集服务器的资源利用率监控,以及Debug服务器代码并解决其效率和稳定性等问题.因此这段时间总结的有关Linux ...

  6. Linux Shell常用技巧(目录)

    Linux Shell常用技巧(一) http://www.cnblogs.com/stephen-liu74/archive/2011/11/10/2240461.html一. 特殊文件: /dev ...

  7. shell常用脚本

    shell常用脚本 author:headsen chen  2017-10-17 15:36:17 个人原创,转载请注明,否则依法追究法律责任 1,vim  name.grep.sh 2,cat   ...

  8. linux 服务器常用命令整理

    linux 服务器常用命令整理 目录 网络分析 - tcpdump \ telnet \ (netstat \ ss \ lsof) \ nload 网络传输 - scp \ rsync \ (rz ...

  9. Linux shell编写脚本部署pxe网络装机

    Linux shell编写脚本部署pxe网络装机 人工安装配置,Linux PXE无人值守网络装机  https://www.cnblogs.com/yuzly/p/10582254.html 脚本实 ...

随机推荐

  1. Mockito学习(zz)

    junitmaven软件测试框架项目管理  Mockito是一个流行的Mocking框架.它使用起来简单,学习成本很低,而且具有非常简洁的API,测试代码的可读性很高.因此它十分受欢迎,用 户群越来越 ...

  2. 数组方法concat & slice

    掌握数组的操作方法: concat() /  slice() concat() 语法: arrayObject.concat(arrayX,arrayY,....,arrayZ) 功能:用于连接两个或 ...

  3. Hibernate 的缓存

    Hibernate的一级缓存 什么是缓存?? 1 数据存到数据库里面,数据库本身是文件系统,使用流方式操作文件效率不是很高.   1.1 把数据存到内存里面,不需要使用流方式,可以直接读取内存中数据 ...

  4. Java 读取HDFS文件系统

    最近有个需求,计算用户画像. 系统大概有800W的用户量,算每个用户的一些数据. 数据量比较大,算用hive还是毫无压力的,但是写的oracle,在给出数据给前端,就比较难受了. 然后换了种解决方法: ...

  5. 2018.11.05 bzoj2143: 飞飞侠(最短路)

    传送门 最短路好题. 考虑对每个二维坐标建立一个高度属性. 这样每次如果在点(i,j,0)(i,j,0)(i,j,0)只能选择花费bi,jb_{i,j}bi,j​跳向(i,j,ai,j)(i,j,a_ ...

  6. vue 开发系列(五) 调用原生API

    概要 我们在开发手机端程序的时候了,我们经常需要使用到拍照,二维码的功能.数字天堂公司提供了大量的原生API支持. http://www.html5plus.org/doc/ 实现 1.在hbuild ...

  7. shell常见命令

    awk awk是个很好用的东西,大量使用在linux系统分析的结果展示处理上.并且可以使用管道, input | awk ''  | output 1.首先要知道形式 awk 'command' fi ...

  8. adb 相关命令 以及无法adb识别设备的解决方法

    [自己解决方法] 在-/.android/文件夹下面新建adb_usb.ini文件.里面写入设备的idVendor号(0x加上四位数字),然后输入 adb kill-server, 然后adb dev ...

  9. mysql学习之路_连接查询

    回顾 列属性:主键,自增长,唯一键. 关系:一对一,一对多,多对多 三层范式: 1NF:字段设计必须符合原子性 2NF:不存在部分依赖(没有复合主键) 3NF:不存在传递依赖(实体单独成表) 逆规范化 ...

  10. mac环境下配置nginx

      1.建议使用homebrew安装(ruby安装 brew install ruby)   ruby -e "$(curl -fsSL https://raw.githubusercont ...