shell脚本:对服务器进行选件并将巡检结果转换为xls文件
getInfo.sh脚本为获取服务器数据:
点击查看代码
#!/bin/sh
##################################
##get the infomation for server ##
##################################
dffile=df.txt
touch /tmp/${dffile}
echo > /tmp/${dffile}
#dfInfo=`df -h |grep -v Filesystem|awk '{print $1,$2,$5,$6}'`
df -h |grep -v Filesystem|awk '{print $1,$2,$5,$6}'> /tmp/${dffile}
#/dev/vda5 96G 29% /
fitstDirInfo=`cat /tmp/${dffile} | sort -k 4 | head -n 1`
#overlay 96G 29% /data/docker/overlay2
daDirInfo=`cat /tmp/${dffile}|grep overlay| sort -k 3 -r|head -n 1`
hostname=`cat /etc/hostname`
ipAddress=`ifconfig|grep -iA1 eth0|grep inet|awk '{print $2}'`
#/dev/mapper/vg_dm_1-lv_dm_1 1.8T 88% /dmdb
#highLevel80=`echo ${dfInfo}|grep -E '8[1-9]%|9[1-9]%'`
touch /tmp/info.json
echo > /tmp/info.json
function FIRSTDIR() {
key1='hostname'
value1=${hostname}
key2='ipaddress'
value2=${ipAddress}
key3='Mounted'
value3=`echo ${fitstDirInfo}|awk '{print $4}'`
key4='totalsize'
value4=`echo ${fitstDirInfo}|awk '{print $2}'`
key5='usedpercent'
value5=`echo ${fitstDirInfo}|awk '{print $3}'`
#echo \{\"${key1}\"\:\"${value1}\"\,\"${key2}\"\:\"${value2}\"\,\"${key3}\"\:\"${value3}\"\,\"${key4}\"\:\"${value4}\"\,\"${key5}\"\:\"${value5}\"\} >> /tmp/info.json
echo "${value1},${value2},${value3},${value4},${value5}" >> /tmp/info.json
}
function HIGH80() {
key1='hostname'
value1=${hostname}
key2='ipaddress'
value2=${ipAddress}
key3='Mounted'
num=`cat /tmp/${dffile} |grep -E '8[1-9]%|9[1-9]%'|wc -l`
if [ $num -gt 0 ]; then
for value3 in `cat /tmp/${dffile} |grep -E '8[1-9]%|9[1-9]%' |awk '{print $4}'|uniq`
do
key4='totalsize'
value4=`cat /tmp/${dffile} |grep -E '8[1-9]%|9[1-9]%'|grep ${value3}|awk '{print $2}'`
key5='usedpercent'
value5=`cat /tmp/${dffile} |grep -E '8[1-9]%|9[1-9]%'|grep ${value3} | awk '{print $3}'`
value6='please clear the disk'
#echo \{\"${key1}\"\:\"${value1}\"\,\"${key2}\"\:\"${value2}\"\,\"${key3}\"\:\"${value3}\"\,\"${key4}\"\:\"${value4}\"\,\"${key5}\"\:\"${value5}\"\} >> /tmp/info.json
echo "${value1},${value2},${value3},${value4},${value5},${value6}" >> /tmp/info.json
done
fi
}
function OVERLAYDIR() {
key1='hostname'
value1=${hostname}
key2='ipaddress'
value2=${ipAddress}
key3='Mounted'
num=`echo ${daDirInfo}|awk '{print $4}'|wc -l`
if [ ${num} -gt 0 ]; then
value3=`echo ${daDirInfo}|awk '{print $4}'`
key4='totalsize'
value4=`echo ${daDirInfo}|awk '{print $2}'`
key5='usedpercent'
value5=`echo ${daDirInfo}|awk '{print $3}'`
#echo \{\"${key1}\"\:\"${value1}\"\,\"${key2}\"\:\"${value2}\"\,\"${key3}\"\:\"${value3}\"\,\"${key4}\"\:\"${value4}\"\,\"${key5}\"\:\"${value5}\"\} >> /tmp/info.json
echo "${value1},${value2},${value3},${value4},${value5}" >> /tmp/info.json
fi
}
function DEALWITH() {
sed -i '/^$/d' /tmp/info.json
rm -f /tmp/${dffile}
}
function getdf() {
FIRSTDIR
HIGH80
OVERLAYDIR
DEALWITH
}
getdf
ansible的yaml文件,用于处理服务器各操作:
点击查看代码
---
- hosts: all
# vars:
# PASSWORD: POWER_scm
gather_facts: false
# vars_prompt:
# - name: "CHANGEPD"
# prompt: "Would you like to change your password,Please enter yes|no"
# #only no can display the msg
# private: no
# default: no
tasks:
# - name: setup demo to get address
# setup:
# filter: "ansible_eth0.ipv4.address"
# tags: always,setups
# register: ipaddress
- name: shell to get address
shell: "ifconfig|grep -iA1 eth0|grep inet|awk '{print $2}'"
tags: always,setups
register: ipaddress2
- name: debug demo1
debug:
#msg: "{{ansible_eth0.ipv4.address}}"
var: ipaddress2.stdout
tags: setups
- name: copy the file /etc/ansible/weiba/scriptdir/getInfo.sh
copy:
src: /etc/ansible/weiba/scriptdir/getInfo.sh
dest: /tmp/getInfo.sh
mode: 744
owner: root
group: root
force: yes
tags: always,getinfo
- name: execute the script getInfo.sh
script:
#/tmp/getInfo.sh
/etc/ansible/weiba/scriptdir/getInfo.sh
tags: always,getinfo
- name: fetch the /tmp/info.json to localDir /etc/ansible/weiba/inspdir/tmpdir
fetch:
src: /tmp/info.json
dest: /etc/ansible/weiba/inspdir/tmpdir/{{ipaddress2.stdout}}-info.json
flat: yes
#with_items:
#- {{ipaddress}}
tags: always,getinfo
ansible role下的task.yaml:
点击查看代码
- name: shell to get address
shell: "ip a |grep inet|grep -vE 'inet6|127.0.0.1|docker|flannel|virbr'|awk '{print $2}'|awk -F '/' '{print $1}'"
register: ipaddress2
tags:
- setup
- always
- getinfo
#- name: debug demo1
# debug:
# #msg: "{{ansible_eth0.ipv4.address}}"
# var: ipaddress2.stdout
#- name: copy the file /etc/ansible/weiba/scriptdir/getInfo.sh
# copy:
# src: /etc/ansible/weiba/scriptdir/getInfo.sh
# dest: /tmp/getInfo.sh
# mode: 744
# owner: root
# group: root
# force: yes
- name: execute the script "{{FILESDIR}}/{{GETINFOSC}}"
script: "{{FILESDIR}}/{{GETINFOSC}}"
tags:
- getinfo
- name: fetch the "{{TMPDIR}}/{{TMPFILE}}" to localDir {{DFTMPDIR}}
fetch:
src: "{{TMPDIR}}/{{TMPFILE}}"
dest: "{{DFTMPDIR}}/{{ipaddress2.stdout}}-{{TMPFILE}}"
flat: yes
tags:
- getinfo
#with_items:
#- {{ipaddress}}
- name: remove the tmp file of "{{TMPDIR}}/{{TMPFILE}}"
file:
#src: "{{TMPDIR}}/{{TMPFILE}}"
state: absent
path: "{{TMPDIR}}/{{TMPFILE}}"
# tags:
# - getserinfo
# - absentfile
#- name: remove the tmp file of "{{TMPDIR}}/{{DFFILE}}"
# file:
# #src: "{{TMPDIR}}/{{DFFILE}}"
# state: absent
# path: "{{TMPDIR}}/{{DFFILE}}"
# tags:
# - getserinfo
# - absentfile
主要的shell脚本,调用ansible的playbook和py脚本:
点击查看代码
#!/bin/sh
######################################
######create a excel file to check####
######################################
curHour=`date +%Y%m%d%H`
curTime=`date +%Y%m%d%H%M`
#the laster layer path
chdir='/etc/ansible/weiba'
python3dir="${chdir}/python3"
palybook='base.yml'
#roles to deal with title
roles1='allnode'
roles2='spsernode'
#the last all result excel path
inspdir="${chdir}/inspdir"
#remote server get info file tmp path
#roles allnode df -h information file path
infofiledir="${inspdir}/tmpdir"
#roles spsernode server os information file path
serverinfordir="${inspdir}/serverinfordir"
alljsonfile1='dfall1.txt'
allserverfile1='serverall2.txt'
#txt exchange to excel name ${xlsname}
xlsname="${curTime}-insp.xls"
#python deal with file name
txtfile='all.txt'
#file type
filejsontype='json'
filetxttype='txt'
#about python
pythonpath='/usr/bin/python'
pythonscript='createexcel.py'
#pytmpdir='/etc/ansible/weiba/python3/tmpdir'
pytmpdir="${python3dir}/tmpdir"
function createfile() {
TMPDIR=$1
TMPFILE=$2
TMPTYPE=$3
if [ $# -lt 3 ]; then exit 110; fi
rm -r ${TMPDIR}/*.${TMPTYPE}
[ -f ${TMPDIR}/${TMPFILE} ] || touch ${TMPDIR}/${TMPFILE}
echo > ${TMPDIR}/${TMPFILE}
}
function cleardir() {
TMPDIR=$1
TMPTYPE=$2
if [ $# -lt 2 ]; then exit 110; fi
cd ${TMPDIR} && rm -r ./*.${TMPTYPE}
}
function ansiblepb() {
cd ${chdir}
#get all json server file
ansible-playbook $1
}
function dealdata() {
#tmpdir
TMPDIR=$1
#tmp file type
TMPTYPE=$2
#one file to add
TMPALLFILE=$3
#roles to deal with title
TMPROLES=$4
#echo > ${alljsonfile1}
cd ${TMPDIR}
#添加汇总文档title
if [ ${TMPROLES} == "${roles1}" ]; then
echo "hostname,ipaddress,Mounted,totalsize,usedpercent,tips" > ${TMPALLFILE}
elif [ ${TMPROLES} == "${roles2}" ]; then
echo "hostname,ipaddress,osversion,cpuprocess,totalmem,usedmem,totalswap,usedswap" > ${TMPALLFILE}
fi
#
for i in `ls *.${TMPTYPE}|grep -v all`
do
cat ${i} >> ${TMPALLFILE}
done
#delete the space
sed -i '/^$/d' ${TMPALLFILE}
}
function createexcel() {
#python script path
TMPDIR=$1
#pytmpdir
PYTMPDIR=$2
#excelname
XLSNAME=$3
#get txt file to exchange excel file
#cd ${TMPDIR} && rm -r ${TXTNAME}
#python tmpdir ${pytmpdir}
[ -d ${PYTMPDIR} ] || mkdir ${PYTMPDIR}
touch ${PYTMPDIR}/testfile.${filetxttype} && cd ${PYTMPDIR} && ls ./*.${filetxttype} |xargs rm -r
#move result file to ${pytmpdir}, would you like to create a function? no I don't
#移动汇总文档到${PYTMPDIR}目录
mv ${infofiledir}/${alljsonfile1} ${PYTMPDIR}
mv ${serverinfordir}/${allserverfile1} ${PYTMPDIR}
#execute the python script ${pythonscript} use python 2.o7
#need module of xlwt and sys
cd ${TMPDIR}
${pythonpath} ${pythonscript} ${PYTMPDIR} ${XLSNAME}
}
function main1() {
#deal with data of roles allnode
createfile ${infofiledir} ${alljsonfile1} ${filejsontype}
#deal with data of roles spsernode
createfile ${serverinfordir} ${allserverfile1} ${filetxttype}
#execute playbook to get data file
ansiblepb ${palybook}
#deal with data of roles allnode
dealdata ${infofiledir} ${filejsontype} ${alljsonfile1} ${roles1}
#deal with data of roles spsernode
dealdata ${serverinfordir} ${filetxttype} ${allserverfile1} ${roles2}
#create the excel file
createexcel ${python3dir} ${pytmpdir} "${inspdir}/${xlsname}"
}
function main() {
#
echo "----------!!!star----------"
echo ""
main1
echo "please download the insp file from ${inspdir}/${curTime}-insp.xls"
echo ""
echo "----------end!!!----------"
}
main