IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> 使用ansible巡检Linux系统并汇总成csv文件 -> 正文阅读

[系统运维]使用ansible巡检Linux系统并汇总成csv文件

collectLog.sh脚本

#!/bin/sh
###############################################################################################################
#Script name: collectLog.sh
#Script description: for Ansible Server collectLog 
#Current Release Version: 1.0.0
#Script Owner: He ,Haibo
#Latest editor: He, Haibo
#Support platform:  Linux OS for redhat and centos.
#Date: 2021/4/21---first Version for collectLog.sh.
#Change log:
#Descript:sh collectLog.sh
#
###############################################################################################################

excuteUser(){
	excuteCurrentUser=`whoami`
	if [[ $excuteCurrentUser == "sysinsp" ]];then
		continue
	else
		echo "Current excute user is $excuteCurrentUser,scripts will exit."
		exit 1
	fi
}

mkdirLogPath(){
    if [[ ! -d /home/sysinsp/LinuxCheck/logs ]];then
        mkdir -p /home/sysinsp/LinuxCheck/logs
        chmod 767 /home/sysinsp/LinuxCheck/logs
    fi
    ansibleLinuxLogPath="/home/sysinsp/LinuxCheck/logs"
    ansibleLinuxLog=ansible.log	
}


copyLinuxShell(){
	ansible -i hosts webservers -m ping >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
	if [[ $? == 0 ]];then
		ansible webservers -i hosts -m copy -a "src=/home/sysinsp/linuxHealthCheck.sh dest=/home/sysinsp/linuxHealthCheck.sh mode=0755" >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
		if [[ $? -ne 0 ]];then
			echo "Cannot copy /home/sysinsp/linuxHealthCheck.sh to dest." "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
			exit 1
		fi
	else
		echo "ansible -i hosts webservers -m copy failed." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
		exit 1
	fi
}

excuteLinuxShell(){
	ansible webservers -i hosts -m shell -a "timeout -k 10s 1m /home/sysinsp/linuxHealthCheck.sh" >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
#	if [[ $? == 0 ]];then
#		echo "ansible webservers -i hosts -m shell -a /home/sysinsp/linuxHealthCheck.sh excute successfully." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
#	else
#		echo "ansible webservers -i hosts -m shell -a /home/sysinsp/linuxHealthCheck.sh excute failed." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
#	fi
}

copyLogFileToLocal(){
	ansible webservers -i hosts -m synchronize -a "mode=pull src=/tmp/log/*_linuxHealthCheck_$(date +%y%m%d).csv dest=$ansibleLinuxLogPath" >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
	if [[ $? == 0 ]];then
		echo "ansible webservers -i hosts -m synchronize -a 'mode=pull src=/tmp/log/*_linuxHealthCheck_$(date +%y%m%d).csv dest=$ansibleLinuxLogPath' successfully." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
	else
		echo "ansible webservers -i hosts -m synchronize -a 'mode=pull src=/tmp/log/*_linuxHealthCheck_$(date +%y%m%d).csv dest=$ansibleLinuxLogPath' failed." >> "$ansibleLinuxLogPath/$ansibleLinuxLog" 2>&1
	fi
}

moveLogs(){
	if [[ ! -d "$ansibleLinuxLogPath"/$(date +%y%m%d) ]];then
		mkdir "$ansibleLinuxLogPath"/$(date +%y%m%d) && cd "$ansibleLinuxLogPath" && mv *_linuxHealthCheck_$(date +%y%m%d).csv "$ansibleLinuxLogPath"/$(date +%y%m%d)
	else
		cd "$ansibleLinuxLogPath" && mv *_linuxHealthCheck_$(date +%y%m%d).csv "$ansibleLinuxLogPath"/$(date +%y%m%d)
	fi
}

generateTotalCsv(){
	if [[ -d "$ansibleLinuxLogPath"/$(date +%y%m%d) ]];then
		cd "$ansibleLinuxLogPath"/$(date +%y%m%d)
		num=1
		echo " " > "$ansibleLinuxLogPath"/$(date +%y%m%d)/linuxHealthCheck_TOTAL_$(date +%y%m%d).csv
		sed -i "$num"c" HOSTNAME , IPADDR , CHECK_DATE , KERNEL , SSH_VERSION , OPENSSL_VERSION , OS_VERSION , UPTIME , SYSTEM_TIME , CPU , MEMORY , SWAP , INODE, DISK , IO , USERS , DEFUNCT , NTP_STATUS , TIMEZONE , DSA_STATUS \n"  linuxHealthCheck_TOTAL_$(date +%y%m%d).csv
		for filename in `ls *_linuxHealthCheck_$(date +%y%m%d).csv | xargs`
		do
			num=$(($num+1))
#			echo $num
			HOSTNAME=`cat $filename | awk -F ',' {'print $1'} | sed 's/[ \t]//g'`
			IPADDR=`cat $filename | awk -F ',' {'print $2'} | sed 's/[ \t]//g'`
			CHECK_DATE=`cat $filename | awk -F ',' {'print $3'} | sed 's/[ \t]//g'`
			KERNEL=`cat $filename | awk -F ',' {'print $4'} | sed 's/[ \t]//g'`
			SSH_VERSION=`cat $filename | awk -F ',' {'print $5'} | sed 's/[ \t]//g'`
			OPENSSL_VERSION=`cat $filename | awk -F ',' {'print $6'} | sed 's/[ \t]//g'`
			OS_VERSION=`cat $filename | awk -F ',' {'print $7'} | sed 's/[ \t]//g'`
			UPTIME=`cat $filename | awk -F ',' {'print $8'} | sed 's/[ \t]//g'`
			SYSTEM_TIME=`cat $filename | awk -F ',' {'print $9'} | sed 's/[ \t]//g'`
			CPU=`cat $filename | awk -F ',' {'print $10'} | sed 's/[ \t]//g'`
			MEMORY=`cat $filename | awk -F ',' {'print $11'} | sed 's/[ \t]//g'`
			SWAP=`cat $filename | awk -F ',' {'print $12'} | sed 's/[ \t]//g'`
			INODE=`cat $filename | awk -F ',' {'print $13'} | sed 's/[ \t]//g'`
			DISK=`cat $filename | awk -F ',' {'print $14'} | sed 's/[ \t]//g'`
			IO=`cat $filename | awk -F ',' {'print $15'} | sed 's/[ \t]//g'`
			USERS=`cat $filename | awk -F ',' {'print $16'} | sed 's/[ \t]//g'`
			DEFUNCT=`cat $filename | awk -F ',' {'print $17'} | sed 's/[ \t]//g'`
			NTP_STATUS=`cat $filename | awk -F ',' {'print $18'} | sed 's/[ \t]//g'`
			TIMEZONE=`cat $filename | awk -F ',' {'print $19'} | sed 's/[ \t]//g'`
			DSA_STATUS=`cat $filename | awk -F ',' {'print $20'} | sed 's/[ \t]//g'`
			#echo "HOSTNAME:$HOSTNAME"
			#echo "IPADDR:$IPADDR"
			#echo "CHECK_DATE:$CHECK_DATE"
			#echo "KERNEL:$KERNEL"
			#echo "SSH_VERSION:$SSH_VERSION"
			#echo "OPENSSL_VERSION:$OPENSSL_VERSION"
			#echo "OS_VERSION:$OS_VERSION"
			#echo "UPTIME:$UPTIME"
			#echo "SYSTEM_TIME:$SYSTEM_TIME"
			#echo "CPU:$CPU"
			#echo "MEMORY:$MEMORY"
			#echo "SWAP:$SWAP"
			#echo "INODE:$INODE"
			#echo "DISK:$DISK"
			#echo "IO:$IO"
			#echo "USERS:$USERS"
			#echo "DEFUNCT:$DEFUNCT"
			#echo "NTP_STATUS:$NTP_STATUS"
			#echo "TIMEZONE:$TIMEZONE"
			sed -i "$num"c" $HOSTNAME, $IPADDR, $CHECK_DATE, $KERNEL, $SSH_VERSION, $OPENSSL_VERSION, $OS_VERSION, $UPTIME, $SYSTEM_TIME, $CPU, $MEMORY, $SWAP, $DISK, $INODE, $IO, $USERS, $DEFUNCT, $NTP_STATUS, $TIMEZONE, $DSA_STATUS \n"  "$ansibleLinuxLogPath"/$(date +%y%m%d)/linuxHealthCheck_TOTAL_$(date +%y%m%d).csv
	done

	fi

}

main(){
	excuteUser
	mkdirLogPath
	copyLinuxShell
	excuteLinuxShell
	copyLogFileToLocal
	moveLogs
	generateTotalCsv
	exit 0
}


main

linuxHealthCheck.sh脚本

#!/bin/sh
###############################################################################################################
#Script name: linuxHealthCheck.sh
#Script description: for Linux Health Check
#Current Release Version: 1.0.0
#Script Owner: He ,Haibo
#Latest editor: He, Haibo
#Support platform:  Linux OS for redhat and centos.
#Date: 2022/4/20---first Version for linuxHealthCheck.sh.
#Change log:2022/4/22 add checkDsa
#Descript:sh linuxHealthCheck.sh
#
###############################################################################################################

export LANG=en_US
osbox=`uname`
RHversion=$(cat /proc/version | sed 's/[^0-9]//g' | cut -b -3)
IPADDR=`/sbin/ifconfig $(netstat -rn |grep ^0.0.0.0|awk '{print $NF}') |grep -w inet|awk '{print $2}'`

###打印日志函数
log_info(){
	DATE_N=`date "+%Y-%m-%d %H:%M:%S"`
	USER_N=`whoami`
	echo "${DATE_N} ${USER_N} execute $0 [INFO] $@"  
}

log_error(){
    DATE_N=`date "+%Y-%m-%d %H:%M:%S"`
    USER_N=`whoami`
    echo -e "\033[41;37m ${DATE_N} ${USER_N} execute $0 [ERROR] $@ \033[0m" 
}

fn_log(){
    if [[ $? -eq 0 ]];then
        log_info "$@ sucessed."
        echo -e "\033[32m $@ sucessed. \033[0m"
    else
        log_error "$@ failed."
        echo -e "\033[41;37m $@ failed. \033[0m"
    fi
}

checkExecuteUser(){
	uid=`id -u`
	if [[ $uid == 0 ]];then
		continue
	else
		fn_log "Current excute user is not root ,shell will exist."
		exit 1
	fi
}

###Create /tmp/log if not exist.
mkdirLogPath(){
    if [[ ! -d /tmp/log ]];then
        mkdir -p /tmp/log
        chmod 767 /tmp/log
    fi
    CheckLinuxLogPath="/tmp/log"
    CheckLinuxLog=`hostname`_${IPADDR}_linuxHealthCheck_$(date +%y%m%d).csv
}

###Get OS Arch Linux or not
getOsArch(){
	if [[ "$osbox" == "Linux" ]];then
		continue
	else
		fn_log "Current OS is $osbox,shell is exit now." >> "$CheckLinuxLogPath/$CheckLinuxLog" 2>&1
		echo 0
		exit 0
	fi
}

###Get redhat or centos
getOsCentosOrRedhat(){
	cat /proc/version | grep -iE "redhat|centos" > /dev/null
	if [[ $? == 0 ]];then
		continue
	else
		echo "Current OS is not centos or redhat." >> "$CheckLinuxLogPath/$CheckLinuxLog" 2>&1
		echo 1
		exit 1
	fi
}


osSystem(){
	OS_HOSTNAME=`hostname`
#	IPADDR=`ifconfig $(netstat -rn |grep ^0.0.0.0|awk '{print $NF}') |grep -w inet|awk '{print $2}'`
	CHECKTIME=`date "+%Y-%m-%d"`
    KERNEL=`uname -r`
	OPENSSH_VERSION=`ssh -V 2>&1|awk '{print $1}' | awk -F ',' {'print $1'}`
	OPENSSL_VERSION=`ssh -V 2>&1|awk '{print $3}'`
	OS_VERSION=`cat /etc/redhat-release | sed 's/[ \t]//g'`
	RUN_TIME=`uptime | awk -F ',' {'print $1'} | awk -F ' ' {'print $3,$4'} | sed 's/[ \t]//g'`
}


#硬件信息
cpuInfo(){
	cpu_idle=$(top -b -n 1 | grep Cpu | awk '{print $8}'| cut -f 1 -d "%" | sed 's/[ \t]//g' | sed "s/\..*//g")
	if [[ $cpu_idle -lt 20 ]]; then
		cpu_idle_status="Failed"
	else
		cpu_idle_status="Success"
	fi
}
##内存信息
memInfo(){
	mem_total=`free -m | grep Mem | awk '{print $2}'`
#	echo "内存总大小为:$mem_total"
	mem_used=`free -m | grep Mem | awk '{print $3}'`
#	echo "已使用内存为:$mem_used"
	mem_free=`free -m | grep Mem | awk '{print $4}'`
#	echo "剩余内存为:$mem_free"
	
	#百分比
	which bc > /dev/null 2>&1
	if [[ $? == 0 ]];then
		mem_used_percent=`echo "scale=2;$mem_used/$mem_total*100"|bc`
		mem_used_percent=`echo $mem_used_percent | awk -F '.' {'print $1'}`
#		echo "已使用内存百分比为:$mem_used_percent"
		if [[ $mem_used_percent -gt 80 ]];then
			mem_used_percent_status="Failed"
		else
			mem_used_percent_status="Success"
		fi
	fi
}

swapInfo(){
	swap_total=`free -m | grep Swap | awk '{print $2}'`
	swap_used=`free -m | grep Swap | awk '{print $3}'`
	which bc > /dev/null 2>&1
	if [[ $? == 0 ]];then
		swap_used_percent=`echo "scale=2;$swap_used/$swap_total*100"|bc`
		swap_used_percent=`echo $swap_used_percent | awk -F '.' {'print $1'}`
		if [[ $swap_used_percent -gt 80 ]];then
			swap_used_percent_status="Failed"
		else
			swap_used_percent_status="Success"
		fi
	fi
}

##get df -h infomation
dfInfo(){
	df_percent=`timeout -k 10 30 df -Th | awk -F '[ |%]+' 'NR>1 {if($6>80) print $6}'`
	if [[ $? == 0 ]];then
		if [[ "$df_percent" == "" ]];then
			df_percent_status="Success"
		else
			df_percent_status="Failed"
		fi
	else
		df_percent_status="Failed"	
	fi
}


checkIOInfo(){
	which iostat > /dev/null 2>&1
	if [[ $? == 0 ]];then
		IOPercent=`iostat | head -4  | awk 'END {print}' | awk -F ' ' {'print $6'} | sed 's/[ \t]//g'`
		IOPercent=`echo $IOPercent | awk -F '.' {'print $1'}`
		if [[ $IOPercent -lt 70 ]];then
			IOPercent_status="Failed"
		else
			IOPercent_status="Success"
		fi
	fi
}

userLogin(){
	userNum=`uptime | awk -F ', ' {'print $3'} | awk -F ' ' {'print $1'} | sed 's/[ \t]//g'`
	if [[ $userNum -gt 10 ]];then
		userNum_status="Failed"
	else
		userNum_status="Success"
	fi
}

checkDefunct(){
	DefunctNum=`ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' | wc -l`
	if [[ $DefunctNum == 0 ]];then
		DefunctNum_status="Success"
	else
		DefunctNum_status="Failed"
	fi
}

checkNtp(){
	ntpstat > /dev/null 2>&1
	if [[ $? == 0 ]];then
		ntpstat_status="Success"
	else
		ntpstat_status="Failed"
	fi
}

checkZone(){
	ZoneName=`timedatectl | grep "Time zone" | awk -F ':' {'print $2'} | awk -F ' ' {'print $1'} | sed 's/[ \t]//g'`
	if [[ $ZoneName == "Asia/Shanghai" ]];then
		ZoneName_status="Success"
	else
		ZoneName_status="Failed"
	fi
}

##get df -i infomation
checkInode(){
    inode_percent=`timeout -k 10 30 df -Ti | awk -F '[ |%]+' 'NR>1 {if($6>80) print $6}'`
    if [[ $? == 0 ]];then
        if [[ "$inode_percent" == "" ]];then
                inode_status="Success"
        else
                inode_status="Failed"
        fi
	else
		inode_status="Failed"
    fi
}


checkDsa(){
	rpm -qa | grep -i "ds_agent" > /dev/null
	if [[ $? == 0 ]];then
		dsa_status="Success"
	else
		dsa_status="Failed"
	fi
}


getSystemTime(){
	DATE_N=`date "+%Y-%m-%d-%H:%M:%S"`
}

totaltocsv(){
	echo "${OS_HOSTNAME} , ${IPADDR} , ${CHECKTIME} , ${KERNEL} , ${OPENSSH_VERSION} , ${OPENSSL_VERSION} , ${OS_VERSION} , ${RUN_TIME} , ${DATE_N}, ${cpu_idle_status} , ${mem_used_percent_status} , ${swap_used_percent_status} , ${df_percent_status} ,${inode_status}, ${IOPercent_status} , ${userNum_status} , ${DefunctNum_status} , ${ntpstat_status} , ${ZoneName_status} , ${dsa_status}" > $CheckLinuxLogPath/$CheckLinuxLog

}

main(){
	getOsArch
	getOsCentosOrRedhat
	mkdirLogPath
	osSystem
	cpuInfo
	memInfo
	swapInfo
	dfInfo
	checkIOInfo
	userLogin
	checkDefunct
	checkNtp
	checkZone
	checkInode
	getSystemTime
	checkDsa
	totaltocsv
	exit 0
}

main

发送邮件脚本

#!/bin/sh
###############################################################################################################
#Script name: sendmail.sh
#Script description: for Linux Health Check
#Current Release Version: 1.0.0
#Script Owner: He ,Haibo
#Latest editor: He, Haibo
#Support platform:  Linux OS for redhat and centos.
#Date: 2022/4/20---first Version for sendmail.sh
#Change log:2022/4/22 add checkDsa
#Descript:sh sendmail.sh
#
###############################################################################################################
mailaddress="1111111@qq.com"

###send linuxHealthCheck_TOTAL_$(date +%y%m%d).csv to mail in $mailaddress
mutt -s "Linux Health Check" -a /home/sysinsp/LinuxCheck/logs/$(date +%y%m%d)/linuxHealthCheck_TOTAL_$(date +%y%m%d).csv -- ${mailaddress} < aa.txt

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-04-26 12:16:02  更:2022-04-26 12:16:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 20:01:40-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码