shell脚本实现-----自动巡检脚本.sh

发布时间:2018-07-22 13:46,

浏览次数:720

, 标签:

shell

sh

[root@localhost ~]# vim自动巡检脚本.sh

#!/bin/bash

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

# Functions: this script from polling system status

# Info: be suitable for CentOS/RHEL 6/7

# Changelog:

#     2018-01-28    Hetl    initial commit

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

#set path env,if not set will some command not found in crontab

# export

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

# source /etc/profile

#set user to run this script

# report_user='monitor'

#  run this script use monitor

# if [ `whoami` != "$report_user" ];then

#     echo " only monitor can run this script"

#     exit 1

# fi

# define log path

# LOGPATH=/home/$report_user/var

# [ -d $LOGPATH ] || mkdir -p $LOGPATH

# RESULTFILE="$LOGPATH/HostDailyCheck-`hostname`-`date +%Y%m%d`.txt"

# check system version,6 or 7 ?

OS_Version=$(awk '{print $(NF-1)}' /etc/redhat-release)

# define globle variable

report_DateTime=''    #日期

report_SystemVersion=''    #系统版本

report_Hostname=''    #主机名

report_IP=''    #主机IP

report_CpuUser=''    #用户空间占用CPU百分比

report_CpuSys=''    #内核空间占用CPU百分比

report_CpuIdle=''    #CPU空闲率

report_CpuUsed=''    #CPU使用率

report_MemALL=''    #内存大小

report_MemFree=''    #内存剩余

report_MenRate=''    #使用率

report_DiskInfo=''    #磁盘信息

report_DefunctProsess=''    #僵尸进程数量

report_ProcessCount=''    #系统进程数

report_ProcessStatus='' #特定进程状态

function getSystemStatus(){

report_DateTime=$(date +"%F %T")

report_SystemVersion=$(cat /etc/redhat-release)

report_Hostname=$(hostname)

}

function getCpuStatus(){

if [[ $OS_Version < 7 ]];then

report_CpuUser=$(top -n 1|grep 'Cpu(s)'|awk '{print $2}'|sed

's/us\,//')

report_CpuSys=$(top -n 1|grep 'Cpu(s)'|awk '{print $3}'|sed

's/sy\,//')

report_CpuIdle=$(top -n 1|grep 'Cpu(s)'|awk '{print $5}'|sed

's/id\,//')

report_CpuUsed=''    #建议程序计算

else

report_CpuUser=$(top -n 1|grep 'Cpu(s)'|awk '{print $2}')%

report_CpuSys=$(top -n 1|grep 'Cpu(s)'|awk '{print $4}')%

report_CpuIdle=$(top -n 1|grep 'Cpu(s)'|awk '{print $8}')%

report_CpuUsed=''    #建议程序计算

fi

}

function getIpInfo(){

#report_IP=$(ip -f inet addr | grep -v 127.0.0.1 |  grep inet | awk

'{print $NF,$2}' | tr '\n' ',' | sed 's/,$//')

if [[ $OS_Version < 7 ]];then

report_IP=$(ifconfig|grep "inet addr:"|grep -v "127.0.0.1"|cut -d:

-f2|awk '{print $1}')

else

report_IP=$(ifconfig|grep -w inet|grep -v "127.0.0.1"|awk '{print

$2}')

fi

}

function getMemInfo(){

report_MemALL=$(grep MemTotal /proc/meminfo|awk {'print $2'})

report_MemFree=$(grep MemFree /proc/meminfo|awk {'print $2'})

report_MenRate=''    #使用率--建议程序计算

}

function getDiskInfo(){

report_DiskInfo=$(df -Ph |grep -vE 'boot|run|tempfs|Filesystem|文件系统'|grep

-vw '0%'|awk '{print $6,$5,$4}')

}

function getProcessNum(){

#僵尸进程数量

report_DefunctProsess="$(ps -ef | grep defunct | grep -v grep|wc -l)"

#进程总数

report_ProcessCount="$(ps aux|wc -l)"

}

#获取单个进程的状态,参数1:程序名,参数2:筛选关键字

function getSingleProcessStats(){

if [ $# -eq 1 ];then

count=$(ps -ef | grep $1 | grep -v 'grep' | wc -l)

elif [[ $# -eq 2 ]]; then

count=$(ps -ef | grep $1 |grep $2| grep -v 'grep' |wc -l)

else

count="ERROR"

fi

if [[ $count = "ERROR" ]]; then

report_ProcessStatus=$report_ProcessStatus$1"

""getSingleProcessStats()参数错误""|"

elif [[ count -ge 1 ]]; then

report_ProcessStatus=$report_ProcessStatus$1" ""YES""|"

else

report_ProcessStatus=$report_ProcessStatus$1" ""NO""|"

fi

}

function getAllProcessStats(){

getSingleProcessStats java CheungSSH aaa

getSingleProcessStats flume

getSingleProcessStats systemd

getSingleProcessStats netns

}

#统一监控平台OVO接口测试

function OVOWebServiceStatus(){

nodeName=`echo -n "192.168.56.200" | base64`

alertName=`echo -n "TEST" | base64`

severity=`echo -n "ALARM" | base64`

Message=`echo -n "OVO接口测试,无需报障,谢谢!" | base64`

#获取当前时间,yyyyMMddHHmmSS

arisingTime=`date +%Y%m%d%H%M%S`

arisingTime=`echo -n "${arisingTime}" | base64`

#目前接口至通过agentId="OVO"的告警

agentId=`echo -n "OVO" | base64`

curl

http://192.168.56.200:8089/collectorAlertService/collectAlert/$nodeName/$alertName/$severity/$Message/$arisingTime/$agentId

if [ $? -eq 0 ];

then

ImmsWebServiceStatus="ACCESS"

echo $ImmsWebServiceStatus

fi

}

function getJson(){

json="{

\"DateTime\":\"$report_DateTime\",

\"report_Hostname\":\"$report_Hostname\",

\"report_IP\":\"$report_IP\",

\"report_SystemVersion\":\"$report_SystemVersion\",

\"report_CpuUser\":\"$report_CpuUser\",

\"report_CpuSys\":\"$report_CpuSys\",

\"report_CpuIdle\":\"$report_CpuIdle\",

\"report_MemALL\":\"$report_MemALL\",

\"report_MemFree\":\"$report_MemFree\",

\"report_DiskInfo\":\"$report_DiskInfo\",

\"report_DefunctProsess\":\"$report_DefunctProsess\",

\"report_ProcessCount\":\"$report_ProcessCount\",

\"report_ProcessStatus\":\"$report_ProcessStatus\"

}"

echo "$json"

}

function main(){

getSystemStatus

getCpuStatus

getIpInfo

getDiskInfo

getMemInfo

getProcessNum

getAllProcessStats

getJson

}

#main > $RESULTFILE

main

shell脚本的效果图

[root@localhost ~]#  sh zhuhaiyan.sh

{

"DateTime":"2018-07-22 13:46:03",

"report_Hostname":"room9pc01",

"report_IP":"176.130.4.34

192.168.4.254

192.168.2.254

201.1.1.254

201.1.2.254

172.25.254.250

172.25.0.250

192.168.6.254

192.168.122.1",

"report_SystemVersion":"CentOS Linux release 7.4.1708 (Core) ",

"report_CpuUser":"28.8%",

"report_CpuSys":"0.0%",

"report_CpuIdle":"97.0%",

"report_MemALL":"16147372",

"report_MemFree":"12032600",

"report_DiskInfo":"/ 75% 29G

/dev/shm 2% 7.6G

/var/ftp/openstack-ext 100% 0

/var/lib/libvirt/images 59% 77G

/var/ftp/centos7 100% 0

/var/www/html/rhel7 100% 0

/var/ftp/rhel7 100% 0

/var/ftp/openstack 100% 0",

"report_DefunctProsess":"0",

"report_ProcessCount":"248",

"report_ProcessStatus":"java getSingleProcessStats()参数错误|flume

NO|systemd YES|netns YES|"

}

canpro脚本_shell脚本实现-----自动巡检脚本.sh相关推荐

  1. Linux自动巡检脚本

    Linux自动巡检脚本 该脚本适用于日常巡检,可根据需求自行增减内容 脚本内容 [root@localhost ~]# cat xunjian-v1.sh #!/bin/bash ########## ...

  2. Linux服务器系统自动巡检脚本生成html报告

    一. 前言 1.在上一篇文章里给大家介绍oracle自动巡检脚本生成html报告,这篇文章介绍linux服务巡检脚本生成html报告. 2.脚本依然是简单脚本语句的堆积,方便大家二次编辑使用. 3.项 ...

  3. linux下的rman自动备份脚本,LINUX上RMAN自动备份脚本

    1. 建备份目录 oradataback,2级目录archback(放归档日志).rmanscripts(备份脚本), 在rmanscripts下面建  rmanlevel0.sh  rmanleve ...

  4. 光遇自动弹琴脚本代码_光遇自动弹琴脚本下载,光遇自动弹琴脚本代码软件 v0.6.2-手游汇...

    光遇自动弹琴脚本是一款趣味跑酷飞行休闲游戏,众多的玩家将会加入到同一片天地中畅游,你可以自由自在地享受飞行,具有非常外观装备随心所欲地供你来选择,装备各种各样的装饰品,去帮助自己的先祖们来完成各种挑战 ...

  5. linux一键安装脚本编写,Linux中自动安装脚本

    如何让你的主机自动的完成安装,自动的回答系统提出的问题,编写自动安装脚本即可实现,如下将详细解释自动脚本安装方法. systemd-config-kickstart  ##自动应答脚本制作工具 安装成 ...

  6. mysql自动备份脚本linux_一个Mysql自动备份脚本,定时备份MYSQ脚本,linux下Mysql自动备份脚本...

    本文转自:http://hi.baidu.com/visual_art/item/6713e2091b41ea33a2332a15 #!/bin/bash #This is a ShellScript ...

  7. Linux基础——”shell脚本编程“ 你知道自动轰炸脚本怎么来的吗?

    文章目录 shell 编程 一.变量 1. shell脚本基础知识 2.shell 编程的基本过程 3. shell变量 二.shell 功能语句 常用功能性语句(命令) 测试语句 三.shell 分 ...

  8. mysql 自动安装脚本_mysql 5.7自动安装脚本

    mysql_version=mysql-5.7.23-linux-glibc2.12-x86_64 install_pack=/root/$mysql_version.tar.gz # 将mysql安 ...

  9. mysql 5.7巡检脚本_mysql自动化巡检脚本生成html报告

    一. 前言 1.之前出了一篇oracle自动巡检脚本生成html报告和一篇Linux服务器系统自动巡检脚本生成html报告,本篇文章出一篇mysql自动巡检脚本生成html报告. 2.脚本只提供部分简 ...

  10. mysql自动化巡检_mysql自动化巡检脚本生成html报告

    一. 前言 1.之前出了一篇oracle自动巡检脚本生成html报告和一篇Linux服务器系统自动巡检脚本生成html报告,本篇文章出一篇mysql自动巡检脚本生成html报告. 2.脚本只提供部分简 ...

最新文章

  1. 2022-2028年中国未硫化橡胶制品行业市场运行格局及未来前景展望报告
  2. VB Mid函数作用
  3. TextView跑步灯效果及在特殊情况下无效的解决方案
  4. angular2 php 教程,有关Material在Angular2中的使用(详细教程)
  5. 1.4 第一个Python程序
  6. 生活中常见的电器,他们的工作原理你知道吗?
  7. libjpeg(2)
  8. 卷积和池化后特征图的大小~
  9. php控制变量的显示字数,3.PHP流程控制结构
  10. python下载-【Python下载 官方版】Python 3.8.0-ZOL软件下载
  11. 精品推荐:【CKEditor】全球最优秀的网页在线文字编辑器之一
  12. 【参赛日记】参加天池大数据竞赛
  13. 2020.11.14--AE--图层八种形式、图层面板图标、查看器面板
  14. 【历史上的今天】5 月 19 日:Java 之父诞生;中国首家互联网公司成立;CP/M 操作系统发明者出生
  15. OpenCV开发笔记(五十八):红胖子8分钟带你深入了解图像的矩(图文并茂+浅显易懂+程序源码)
  16. MacM1安装homebrew步骤
  17. 设计师:设计师知识储备之室内设计风格图文介绍大全(中式风格、清新风格、现代简约、现代风格、后现代风格、田园风格-中式田园-欧式田园-美式田园-美式乡村风格)之详细攻略
  18. 轻量级复式记账工具Beancount推荐使用心得
  19. 理解 operator=
  20. MYSQL必知必会 第四章 检索数据

热门文章

  1. 电机驱动L298N调试的问题。
  2. php除数不能为零,0为什么不能做除数(为什么0不能作为除数)
  3. java数据库驱动加载失败_Java连接数据库,成功加载SQL驱动程序,但数据库连接失败...
  4. ES6最详细/易懂教程
  5. PHP CURL 使用代理 获取国外资源
  6. 谷歌gmail注册入口_Gmail样式的侧面板即将用于Google文档和Google日历
  7. 注册gmail邮箱手机号无法验证
  8. 身为码农,为12306说两句公道话
  9. isee看图精灵下载
  10. 基于STM32的DDS信号发生器