Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23952
  • 博文数量: 4
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2018-04-14 11:46
个人简介

专业打杂,挨踢

文章分类

全部博文(4)

文章存档

2018年(4)

我的朋友

分类: 系统运维

2018-05-08 17:56:32

项目需要收集服务器一些进程,计划任务和软件版本信息。
下面为相应的SHELL代码,仅供参考。

#!/bin/bash
source /etc/profile
source ~/.bash_profile
#部署路径
path="/usr/local/japp"


portfile="port.txt"
versionfile="version.txt"
processfile="process.txt"
cronfile="task.txt"


#重复执行先清空上次结果
cat /dev/null > $path/$portfile
cat /dev/null > $path/$versionfile
cat /dev/null > $path/$cronfile
#建立文件索引
updatedb
#收集进程信息
ps -eo pid,user,size,vsize,nice,stat,pcpu,pmem,fname > $path/$processfile
LANG=C sed -i '1,1d' $path/$processfile


#收集端口号和进程信息
netstat -lnptuw|grep -v grep>$path/netstat_status.txt
LANG=C sed -i '1,2d' $path/netstat_status.txt
cat $path/netstat_status.txt|awk -F " " '{print $1 " " $4 " " $6 " " $7}'>$path/port_pid.txt
#echo "protocol,port,process_id,command_name,exe_location"
while read line
do
  protocol=`echo $line |awk -F " " '{print $1}'`
  port=`echo $line |awk -F " " '{print $2}'|awk -F ":" '{print $NF}'`


if [ $protocol = "udp" ];then
 server_pid=`echo $line |awk -F " " '{print $3}'|awk -F "/" '{print $1}'`
 command_name=`echo $line |awk -F " " '{print $3}'|awk -F "/" '{print $NF}'`
else
 server_pid=`echo $line |awk -F " " '{print $4}'|awk -F "/" '{print $1}'`
 command_name=`echo $line |awk -F " " '{print $4}'|awk -F "/" '{print $NF}'`
fi
 exe_location=`ls -la /proc/$server_pid|grep "exe ->"|grep -v grep|awk '{print $NF}'`
 echo $protocol,$port,$server_pid,$command_name,$exe_location >> $path/$portfile
done<$path/port_pid.txt
rm -rf $path/netstat_status.txt
rm -rf $path/port_pid.txt


#收集crontab任务信息
ls -1 /var/spool/cron > $path/tmp.txt
while read line
do
cat /var/spool/cron/$line|grep -v "SHELL" >> $path/$cronfile
done<$path/tmp.txt




#收集版本信息
#redis
redis_path=$(ps -ef | grep -v grep | grep redis | awk '{print $8}')
if [ A$redis_path != A ];then
$(whereis $redis_path|awk -F " " '{print $2}') -v|awk '{print $1 " " $3}' > $path/tmp.txt
LANG=C sed -i 's/v=//g' $path/tmp.txt
redisver=$(cat $path/tmp.txt) 
echo "$redisver $redis_path" >> $path/$versionfile
fi

#nginx
nginx_path=$(ps -ef|grep -v grep | grep -E  'master.*nginx'|awk '{print $11}') 
nginx_info=$($nginx_path -v 2>&1)
echo $nginx_info > $path/tmp.txt
if [ A$nginx_path != A ];then
nginx_version=$(cat $path/tmp.txt|awk -F " " '{print $3}'|awk -F "/" '{print $2}')
nginx_name="Nginx "
if [ A$nginx_version != A ];then
echo "$nginx_name$nginx_version $nginx_path" >> $path/$versionfile
fi
fi

#tomcat
ps -ef|grep tomcat|grep -v grep|awk '{print $2}'|xargs pwdx | awk '{print $2}'|uniq > $path/tmp.txt
tomcat_path=$(ps -ef |grep tomcat|grep -v grep | awk '{print $2}'|xargs pwdx|awk '{print $2}'|head -1)
if [ A$tomcat_path != A ];then
while read line
do
tomcat_version=$(cd $line && sh version.sh|awk '/Server version/{gsub("/"," ",$4);print$4}')
if [ A"$tomcat_version" != A ];then
echo "$tomcat_version $line" >> $path/$versionfile
fi
done<$path/tmp.txt
fi

#was
ps -ef|grep -i 'WebSphere'|grep -v grep|awk '{print $2}'|xargs pwdx > /dev/null 2>&1
if [ "$?" = 0 ];then
was_path=$(locate SystemOut.log|head -1)
if [ A$was_path != A ];then
wasver=$(cat $was_path |grep "WebSphere Platform"|awk -F " " '{print $1 " " $5}'|head -1)
waspath=$(locate SystemOut.log|head -1|awk -F "/" '{print "/"$2"/"$3"/"$4"/"$5"/"$6}')
echo $wasver $waspath >> $path/$versionfile
fi
fi

#weblogic
ps -ef|grep startNodeManager|grep -v grep|awk '{print $2}'|xargs pwdx > /dev/null 2>&1
if [ "$?" = 0 ];then
weblogic_path=$(locate -r "/\registry.xml"|head -1)
if [ A$weblogic_path != A ];then
cat $weblogic_path |grep -E 'WebLogic Server.*version'|awk -F "\"" '{print $2$4 " " $6}' > $path/tmp.txt
LANG=C sed -i 's/Server//g' $path/tmp.txt
cat $path/tmp.txt >> $path/$versionfile
fi
fi

#apache
ps -ef | grep -E 'apache.*httpd'| grep -v grep|awk '{print $2}'|xargs pwdx > /dev/null 2>&1
if [ "$?" = 0 ];then
apache_path=$(locate -r "/\apachectl"|grep bin|head -1)
if [ A$apache_path != A ];then
apaver=$($apache_path -v|head -1|awk -F " " '{print $3}'|awk -F "/" '{print $1 " " $2}')
if [ A"$apaver" != A ];then
echo "$apaver $apache_path">> $path/$versionfile
fi
fi
fi

#vsftp
vsftp_path=$(ps -ef|grep vsftp|grep -v grep|awk '{print $8}')
if [ A$vsftp_path != A ];then
strings $vsftp_path|awk '/vsftpd: version/{gsub(": version","",$0);print$0}' >> $path/$versionfile
fi

#openssh
ssh_path=$(ps -ef | grep '/sshd'|grep -v grep|awk '{print $8}')
$ssh_path -V &>$path/tmp.txt
cat $path/tmp.txt | sed -n '2p' |awk -F "," '{print $1}'|awk -F "_" '{print $1 " " $2}' >> $path/$versionfile

#openssl
openssl version >$path/tmp.txt
cat $path/tmp.txt|awk -F " " '{print $1 " " $2}' >>  $path/$versionfile

#mysql
mysql_sbin=$(ps -ef|grep mysqld|grep -v mysqld_safe|grep -v grep|awk '{print $8}')
mysql_pid=$(ps -ef|grep mysqld|grep -v mysqld_safe|grep -v grep|awk '{print $2}')
if [ A$mysql_sbin != A ];then
mysqlver=$($mysql_sbin -V|awk '{print "mysql"" "$3}') 
mysqlpath=$(pwdx $mysql_pid|awk '{print $2}')
if [ A"$mysqlver" != A ];then
echo "$mysqlver $mysqlpath" >>  $path/$versionfile
fi
fi

#JDK
ps -ef|grep jdk|grep -v grep|awk '{print $8}'|uniq > $path/tmp.txt
jdk_path=$(ps -ef|grep jdk|grep -v grep|awk '{print $8}'|head -1)

if [ A$jdk_path != A ];then
while read line
do
$line -version 2>&1 |less > $path/jdktmp.txt
jdk_ver=$(cat $path/jdktmp.txt |awk -F "\"" '/java version/{print $2}')
echo "$jdk_ver $line" >> $path/jdkverpathtmp.txt
echo "$jdk_ver" >> $path/jdkvertmp.txt
done<$path/tmp.txt
sort -k2n $path/jdkvertmp.txt | awk '{if ($0!=line) print;line=$0}' >> $path/jdkvertmp2.txt

while read line2
do
       cat $path/jdkverpathtmp.txt|grep -w $line2 |awk '{print $2}'>$path/path.txt
       path2=$(cat $path/path.txt |xargs | sed 's/ /,/g')
       echo "JDK $line2 $path2" >> $path/$versionfile
done<$path/jdkvertmp2.txt
fi
rm -rf $path/jdkvertmp2.txt
rm -rf $path/jdkvertmp.txt
rm -rf $path/jdkverpathtmp.txt
rm -rf $path/jdktmp.txt
rm -rf $path/tmp.txt
rm -rf $path/path.txt
unset line2
unset line
unset path2


#Struts2
locate jar|grep struts2|grep -o "[0-9]\{1\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}"|sort -k2n|awk '{if ($0!=line) print;line=$0}' > $path/tmp.txt
content=$(cat $path/tmp.txt |head -1)
if [ A$content != A ];then
while read line
do
     #输出匹配版本号的jar路径 
locate jar|grep struts|grep -w $line >> $path/path.txt
     #截取到lib的路径
while read line2
do
echo  ${line2%/*} >> $path/path2.txt
done < $path/path.txt
#去除重复
cat $path/path2.txt |sort -k2n|awk '{if ($0!=line2) print;line2=$0}' > $path/path3.txt
     #合并成一行
path2=$(cat $path/path3.txt |xargs | sed 's/ /,/g')
#输出版本和路径
echo "Struts2 $line $path2" >> $path/$versionfile
done < $path/tmp.txt
fi
rm -rf $path/path2.txt
rm -rf $path/path3.txt
rm -rf $path/path.txt
unset line2
unset line
unset path2


#Spring
locate -r "spring-web.*.jar$"|grep -o "[s][p][r][i][n][g].*"|grep -o "[0-9]\{1\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}"|sort -k2n|awk '{if ($0!=line) print;line=$0}' > $path/tmp.txt
content=$(cat $path/tmp.txt |head -1)
if [ A$content != A ];then
while read line
do
     #输出匹配版本号的jar路径 
locate -r "spring-web.*.jar$" |grep -w $line >> $path/path.txt
     #截取到lib的路径
while read line2
do
echo  ${line2%/*} >> $path/path2.txt
done < $path/path.txt
#去除重复
cat $path/path2.txt |sort -k2n|awk '{if ($0!=line2) print;line2=$0}' > $path/path3.txt
     #合并成一行
path2=$(cat $path/path3.txt |xargs | sed 's/ /,/g')
#输出版本和路径
echo "Spring $line $path2" >> $path/$versionfile
done < $path/tmp.txt


fi
rm -rf $path/path2.txt
rm -rf $path/path3.txt
rm -rf $path/path.txt
##
rm -rf $path/tmp.txt


阅读(1956) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~