Linux 系统日常巡检脚本,


Linux 系统日常巡检脚本,巡检内容包含了,磁盘,内存 cpu 进程 文件更改 用户登录等一系列的操作 直接用就行了。

报告以邮件发送到邮箱 在log下生成巡检报告。

  1. #!/bin/bash  
  2. # @Author: HanWei  
  3. # @Date:   2020-03-16 09:56:57  
  4. # @Last Modified by:   HanWei  
  5. # @Last Modified time: 2020-03-16 11:06:31  
  6. # @E-mail: han_wei_95@163.com  
  7. #!/bin/bash  
  8. #主机信息每日巡检  
  9. IPADDR=$(ifconfig eth0|grep 'inet addr'|awk -F '[ :]' '{print $13}')  
  10. #环境变量PATH没设好,在cron里执行时有很多命令会找不到  
  11. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin  
  12. source /etc/profile 
  13. [ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1  
  14. centosVersion=$(awk '{print $(NF-1)}' /etc/redhat-release)  
  15. VERSION="2020-03-16"  
  16. #日志相关  
  17. PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`  
  18. [ -f $PROGPATH ] && PROGPATH="."  
  19. LOGPATH="$PROGPATH/log" 
  20. [ -e $LOGPATH ] || mkdir $LOGPATH  
  21. RESULTFILE="$LOGPATH/HostDailyCheck-$IPADDR-`date +%Y%m%d`.txt"  
  22. #定义报表的全局变量  
  23. report_DateTime="" #日期 ok  
  24. report_Hostname="" #主机名 ok  
  25. report_OSRelease="" #发行版本 ok  
  26. report_Kernel="" #内核 ok  
  27. report_Language="" #语言/编码 ok  
  28. report_LastReboot="" #最近启动时间 ok  
  29. report_Uptime="" #运行时间(天) ok  
  30. report_CPUs="" #CPU数量 ok  
  31. report_CPUType="" #CPU类型 ok  
  32. report_Arch="" #CPU架构 ok  
  33. report_MemTotal="" #内存总容量(MB) ok  
  34. report_MemFree="" #内存剩余(MB) ok  
  35. report_MemUsedPercent="" #内存使用率% ok  
  36. report_DiskTotal="" #硬盘总容量(GB) ok  
  37. report_DiskFree="" #硬盘剩余(GB) ok  
  38. report_DiskUsedPercent="" #硬盘使用率% ok  
  39. report_InodeTotal="" #Inode总量 ok  
  40. report_InodeFree="" #Inode剩余 ok  
  41. report_InodeUsedPercent="" #Inode使用率 ok  
  42. report_IP="" #IP地址 ok  
  43. report_MAC="" #MAC地址 ok  
  44. report_Gateway="" #默认网关 ok  
  45. report_DNS="" #DNS ok  
  46. report_Listen="" #监听 ok  
  47. report_Selinux="" #Selinux ok  
  48. report_Firewall="" #防火墙 ok  
  49. report_USERs="" #用户 ok  
  50. report_USEREmptyPassword="" #空密码用户 ok  
  51. report_USERTheSameUID="" #相同ID的用户 ok   
  52. report_PasswordExpiry="" #密码过期(天) ok  
  53. report_RootUser="" #root用户 ok  
  54. report_Sudoers="" #sudo授权 ok  
  55. report_SSHAuthorized="" #SSH信任主机 ok  
  56. report_SSHDProtocolVersion="" #SSH协议版本 ok  
  57. report_SSHDPermitRootLogin="" #允许root远程登录 ok  
  58. report_DefunctProsess="" #僵尸进程数量 ok  
  59. report_SelfInitiatedService="" #自启动服务数量 ok  
  60. report_SelfInitiatedProgram="" #自启动程序数量 ok  
  61. report_RuningService="" #运行中服务数 ok  
  62. report_Crontab="" #计划任务数 ok  
  63. report_Syslog="" #日志服务 ok  
  64. report_SNMP="" #SNMP OK  
  65. report_NTP="" #NTP ok  
  66. report_JDK="" #JDK版本 ok  
  67. function version(){  
  68. echo ""  
  69. echo ""  
  70. echo "系统巡检脚本:Version $VERSION"  
  71. }  
  72. function getCpuStatus(){  
  73. echo ""  
  74. echo ""  
  75. echo "############################ CPU检查 #############################"  
  76. Physical_CPUs=$(grep "physical id" /proc/cpuinfo| sort | uniq | wc -l)  
  77. Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l)  
  78. CPU_Kernels=$(grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}')  
  79. CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)  
  80. CPU_Arch=$(uname -m)  
  81. echo "物理CPU个数:$Physical_CPUs"  
  82. echo "逻辑CPU个数:$Virt_CPUs"  
  83. echo "每CPU核心数:$CPU_Kernels"  
  84. echo " CPU型号:$CPU_Type"  
  85. echo " CPU架构:$CPU_Arch"  
  86. #报表信息  
  87. report_CPUs=$Virt_CPUs #CPU数量  
  88. report_CPUType=$CPU_Type #CPU类型  
  89. report_Arch=$CPU_Arch #CPU架构  
  90. }  
  91. function getMemStatus(){  
  92. echo ""  
  93. echo ""  
  94. echo "############################ 内存检查 ############################"  
  95. if [[ $centosVersion < 7 ]];then  
  96. free -mo  
  97. else  
  98. free -h  
  99. fi  
  100. #报表信息  
  101. MemTotal=$(grep MemTotal /proc/meminfo| awk '{print $2}') #KB  
  102. MemFree=$(grep MemFree /proc/meminfo| awk '{print $2}') #KB  
  103. let MemUsed=MemTotal-MemFree  
  104. MemPercent=$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")  
  105. report_MemTotal="$((MemTotal/1024))""MB" #内存总容量(MB)  
  106. report_MemFree="$((MemFree/1024))""MB" #内存剩余(MB)  
  107. report_MemUsedPercent="$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")""%" #内存使用率%  
  108. }  
  109. function getDiskStatus(){  
  110. echo ""  
  111. echo ""  
  112. echo "############################ 磁盘检查 ############################" 
  113. df -hiP | sed 's/Mounted on/Mounted/'> /tmp/inode  
  114. df -hTP | sed 's/Mounted on/Mounted/'> /tmp/disk   
  115. join /tmp/disk /tmp/inode | awk '{print $1,$2,"|",$3,$4,$5,$6,"|",$8,$9,$10,$11,"|",$12}'| column -t  
  116. #报表信息  
  117. diskdata=$(df -TP | sed '1d' | awk '$2!="tmpfs"{print}') #KB  
  118. disktotal=$(echo "$diskdata" | awk '{total+=$3}END{print total}') #KB  
  119. diskused=$(echo "$diskdata" | awk '{total+=$4}END{print total}') #KB  
  120. diskfree=$((disktotal-diskused)) #KB  
  121. diskusedpercent=$(echo $disktotal $diskused | awk '{if($1==0){printf 100}else{printf "%.2f",$2*100/$1}}')   
  122. inodedata=$(df -iTP | sed '1d' | awk '$2!="tmpfs"{print}')  
  123. inodetotal=$(echo "$inodedata" | awk '{total+=$3}END{print total}')  
  124. inodeused=$(echo "$inodedata" | awk '{total+=$4}END{print total}')  
  125. inodefree=$((inodetotal-inodeused))  
  126. inodeusedpercent=$(echo $inodetotal $inodeused | awk '{if($1==0){printf 100}else{printf "%.2f",$2*100/$1}}')  
  127. report_DiskTotal=$((disktotal/1024/1024))"GB" #硬盘总容量(GB)  
  128. report_DiskFree=$((diskfree/1024/1024))"GB" #硬盘剩余(GB)  
  129. report_DiskUsedPercent="$diskusedpercent""%" #硬盘使用率%  
  130. report_InodeTotal=$((inodetotal/1000))"K" #Inode总量  
  131. report_InodeFree=$((inodefree/1000))"K" #Inode剩余  
  132. report_InodeUsedPercent="$inodeusedpercent""%" #Inode使用率%  
  133. function getSystemStatus(){  
  134. echo ""  
  135. echo ""  
  136. echo "############################ 系统检查 ############################"  
  137. if [ -e /etc/sysconfig/i18n ];then  
  138. default_LANG="$(grep "LANG=" /etc/sysconfig/i18n | grep -v "^#" | awk -F '"' '{print $2}')"  
  139. else  
  140. default_LANG=$LANG  
  141. fi  
  142. export LANG="en_US.UTF-8"  
  143. Release=$(cat /etc/redhat-release 2>/dev/null)  
  144. Kernel=$(uname -r)  
  145. OS=$(uname -o)  
  146. Hostname=$(uname -n)  
  147. SELinux=$(/usr/sbin/sestatus | grep "SELinux status: " | awk '{print $3}')  
  148. LastReboot=$(who -b | awk '{print $3,$4}')  
  149. uptime=$(uptime | sed 's/.*up \([^,]*\), .*/\1/')  
  150. echo " 系统:$OS"  
  151. echo " 发行版本:$Release"  
  152. echo " 内核:$Kernel"  
  153. echo " 主机名:$Hostname"  
  154. echo " SELinux:$SELinux"  
  155. echo "语言/编码:$default_LANG"  
  156. echo " 当前时间:$(date +'%F %T')"  
  157. echo " 最后启动:$LastReboot"  
  158. echo " 运行时间:$uptime"  
  159. #报表信息  
  160. report_DateTime=$(date +"%F %T") #日期  
  161. report_Hostname="$Hostname" #主机名  
  162. report_OSRelease="$Release" #发行版本  
  163. report_Kernel="$Kernel" #内核  
  164. report_Language="$default_LANG" #语言/编码  
  165. report_LastReboot="$LastReboot" #最近启动时间  
  166. report_Uptime="$uptime" #运行时间(天)  
  167. report_Selinux="$SELinux"  
  168. export LANG="$default_LANG" 
  169. function getServiceStatus(){  
  170. echo ""  
  171. echo ""  
  172. echo "############################ 服务检查 ############################"  
  173. echo ""  
  174. if [[ $centosVersion > 7 ]];then  
  175. conf=$(systemctl list-unit-files --type=service --state=enabled --no-pager | grep "enabled")  
  176. process=$(systemctl list-units --type=service --state=running --no-pager | grep ".service")  
  177. #报表信息  
  178. report_SelfInitiatedService="$(echo "$conf" | wc -l)" #自启动服务数量  
  179. report_RuningService="$(echo "$process" | wc -l)" #运行中服务数量  
  180. else  
  181. conf=$(/sbin/chkconfig | grep -E ":on|:启用")  
  182. process=$(/sbin/service --status-all 2>/dev/null | grep -E "is running|正在运行")  
  183. #报表信息  
  184. report_SelfInitiatedService="$(echo "$conf" | wc -l)" #自启动服务数量  
  185. report_RuningService="$(echo "$process" | wc -l)" #运行中服务数量  
  186. fi  
  187. echo "服务配置"  
  188. echo "--------"  
  189. echo "$conf" | column -t  
  190. echo ""  
  191. echo "正在运行的服务"  
  192. echo "--------------"  
  193. echo "$process" 
  194. }  
  195. function getAutoStartStatus(){  
  196. echo ""  
  197. echo ""  
  198. echo "############################ 自启动检查 ##########################"  
  199. conf=$(grep -v "^#" /etc/rc.d/rc.local| sed '/^$/d')  
  200. echo "$conf"  
  201. #报表信息  
  202. report_SelfInitiatedProgram="$(echo $conf | wc -l)" #自启动程序数量  
  203. function getLoginStatus(){  
  204. echo ""  
  205. echo ""  
  206. echo "############################ 登录检查 ############################"  
  207. last | head  
  208. function getNetworkStatus(){  
  209. echo ""  
  210. echo ""  
  211. echo "############################ 网络检查 ############################"  
  212. if [[ $centosVersion < 7 ]];then  
  213. /sbin/ifconfig -a | grep -v packets | grep -v collisions | grep -v inet6  
  214. else  
  215. #ip a  
  216. for i in $(ip link | grep BROADCAST | awk -F: '{print $2}');do ip add show $i | grep -E "BROADCAST|global"| awk '{print $2}' | tr '\n' ' ' ;echo "" ;done  
  217. fi  
  218. GATEWAY=$(ip route | grep default | awk '{print $3}')  
  219. DNS=$(grep nameserver /etc/resolv.conf| grep -v "#" | awk '{print $2}' | tr '\n' ',' | sed 's/,$//')  
  220. echo ""  
  221. echo "网关:$GATEWAY "  
  222. echo " DNS:$DNS"  
  223. #报表信息  
  224. IP=$(ip -f inet addr | grep -v 127.0.0.1 | grep inet | awk '{print $NF,$2}' | tr '\n' ',' | sed 's/,$//')  
  225. MAC=$(ip link | grep -v "LOOPBACK\|loopback" | awk '{print $2}' | sed 'N;s/\n//' | tr '\n' ',' | sed 's/,$//')  
  226. report_IP="$IP" #IP地址  
  227. report_MAC=$MAC #MAC地址  
  228. report_Gateway="$GATEWAY" #默认网关  
  229. report_DNS="$DNS" #DNS  
  230. function getListenStatus(){  
  231. echo ""  
  232. echo ""  
  233. echo "############################ 监听检查 ############################"  
  234. TCPListen=$(ss -ntul | column -t)  
  235. echo "$TCPListen"  
  236. #报表信息  
  237. report_Listen="$(echo "$TCPListen"| sed '1d' | awk '/tcp/ {print $5}' | awk -F: '{print $NF}' | sort | uniq | wc -l)"  
  238. function getCronStatus(){  
  239. echo ""  
  240. echo ""  
  241. echo "############################ 计划任务检查 ########################"  
  242. Crontab=0  
  243. for shell in $(grep -v "/sbin/nologin" /etc/shells);do  
  244. for user in $(grep "$shell" /etc/passwd| awk -F: '{print $1}');do  
  245. crontab -l -u $user >/dev/null 2>&1  
  246. status=$?  
  247. if [ $status -eq 0 ];then  
  248. echo "$user"  
  249. echo "--------"  
  250. crontab -l -u $user  
  251. let CrontabCrontab=Crontab+$(crontab -l -u $user | wc -l)  
  252. echo ""  
  253. fi  
  254. done  
  255. done  
  256. #计划任务  
  257. find /etc/cron* -type f | xargs -i ls -l {} | column -t 
  258. let CrontabCrontab=Crontab+$(find /etc/cron* -type f | wc -l)  
  259. #报表信息  
  260. report_Crontab="$Crontab" #计划任务数  
  261. }  
  262. function getHowLongAgo(){  
  263. # 计算一个时间戳离现在有多久了  
  264. datetime="$*"  
  265. [ -z "$datetime" ] && echo "错误的参数:getHowLongAgo() $*"  
  266. Timestamp=$(date +%s -d "$datetime") #转化为时间戳  
  267. Now_Timestamp=$(date +%s) 
  268. Difference_Timestamp=$(($Now_Timestamp-$Timestamp)) 
  269. days=0;hours=0;minutes=0;  
  270. sec_in_day=$((60*60*24));  
  271. sec_in_hour=$((60*60));  
  272. sec_in_minute=60  
  273. while (( $(($Difference_Timestamp-$sec_in_day)) > 1 ))  
  274. do  
  275. let Difference_TimestampDifference_Timestamp=Difference_Timestamp-sec_in_day  
  276. let days++  
  277. done  
  278. while (( $(($Difference_Timestamp-$sec_in_hour)) > 1 ))  
  279. do  
  280. let Difference_TimestampDifference_Timestamp=Difference_Timestamp-sec_in_hour  
  281. let hours++  
  282. done  
  283. echo "$days 天 $hours 小时前"  
  284. function getUserLastLogin(){  
  285. # 获取用户最近一次登录的时间,含年份  
  286. # 很遗憾last命令不支持显示年份,只有"last -t YYYYMMDDHHMMSS"表示某个时间之间的登录,我  
  287. # 们只能用最笨的方法了,对比今天之前和今年元旦之前(或者去年之前和前年之前……)某个用户  
  288. # 登录次数,如果登录统计次数有变化,则说明最近一次登录是今年。  
  289. username=$1  
  290. : ${username:="`whoami`"}  
  291. thisYear=$(date +%Y)  
  292. oldesYear=$(last | tail -n1 | awk '{print $NF}')  
  293. while(( $thisYear >= $oldesYear));do  
  294. loginBeforeToday=$(last $username | grep $username | wc -l)  
  295. loginBeforeNewYearsDayOfThisYear=$(last $username -t $thisYear"0101000000" | grep $username | wc -l)  
  296. if [ $loginBeforeToday -eq 0 ];then  
  297. echo "从未登录过"  
  298. break  
  299. elif [ $loginBeforeToday -gt $loginBeforeNewYearsDayOfThisYear ];then  
  300. lastDateTime=$(last -i $username | head -n1 | awk '{for(i=4;i<(NF-2);i++)printf"%s ",$i}')" $thisYear" #格式如: Sat Nov 2 20:33 2015  
  301. lastDateTime=$(date "+%Y-%m-%d %H:%M:%S" -d "$lastDateTime")  
  302. echo "$lastDateTime"  
  303. break  
  304. else  
  305. thisYear=$((thisYear-1))  
  306. fi  
  307. done 
  308. function getUserStatus(){  
  309. echo ""  
  310. echo ""  
  311. echo "############################ 用户检查 ############################"  
  312. #/etc/passwd 最后修改时间  
  313. pwdfile="$(cat /etc/passwd)"  
  314. Modify=$(stat /etc/passwd | grep Modify | tr '.' ' ' | awk '{print $2,$3}') 
  315. echo "/etc/passwd 最后修改时间:$Modify ($(getHowLongAgo $Modify))"  
  316. echo ""  
  317. echo "特权用户"  
  318. echo "--------"  
  319. RootUser=""  
  320. for user in $(echo "$pwdfile" | awk -F: '{print $1}');do  
  321. if [ $(id -u $user) -eq 0 ];then  
  322. echo "$user"  
  323. RootUser="$RootUser,$user"  
  324. fi  
  325. done  
  326. echo ""  
  327. echo "用户列表"  
  328. echo "--------"  
  329. USERs=0  
  330. echo "$(  
  331. echo "用户名 UID GID HOME SHELL 最后一次登录"  
  332. for shell in $(grep -v "/sbin/nologin" /etc/shells);do  
  333. for username in $(grep "$shell" /etc/passwd| awk -F: '{print $1}');do  
  334. userLastLogin="$(getUserLastLogin $username)"  
  335. echo "$pwdfile" | grep -w "$username" |grep -w "$shell"| awk -F: -v lastlogin="$(echo "$userLastLogin" | tr ' ' '_')" '{print $1,$3,$4,$6,$7,lastlogin}'  
  336. done  
  337. let USERsUSERs=USERs+$(echo "$pwdfile" | grep "$shell"| wc -l)  
  338. done  
  339. )" | column -t  
  340. echo ""  
  341. echo "空密码用户"  
  342. echo "----------"  
  343. USEREmptyPassword=""  
  344. for shell in $(grep -v "/sbin/nologin" /etc/shells);do  
  345. for user in $(echo "$pwdfile" | grep "$shell" | cut -d: -f1);do  
  346. r=$(awk -F: '$2=="!!"{print $1}' /etc/shadow | grep -w $user)  
  347. if [ ! -z $r ];then  
  348. echo $r  
  349. USEREmptyPassword="$USEREmptyPassword,"$r  
  350. fi  
  351. done   
  352. done  
  353. echo ""  
  354. echo "相同ID的用户"  
  355. echo "------------"  
  356. USERTheSameUID=""  
  357. UIDs=$(cut -d: -f3 /etc/passwd | sort | uniq -c | awk '$1>1{print $2}')  
  358. for uid in $UIDs;do  
  359. echo -n "$uid";  
  360. USERTheSameUID="$uid"  
  361. r=$(awk -F: 'ORS="";$3=='"$uid"'{print ":",$1}' /etc/passwd)  
  362. echo "$r"  
  363. echo ""  
  364. USERTheSameUID="$USERTheSameUID $r,"  
  365. done  
  366. #报表信息  
  367. report_USERs="$USERs" #用户  
  368. report_USEREmptyPassword=$(echo $USEREmptyPassword | sed 's/^,//')   
  369. report_USERTheSameUID=$(echo $USERTheSameUID | sed 's/,$//')   
  370. report_RootUser=$(echo $RootUser | sed 's/^,//') #特权用户  
  371. function getPasswordStatus {  
  372. echo ""  
  373. echo ""  
  374. echo "############################ 密码检查 ############################"  
  375. pwdfile="$(cat /etc/passwd)"  
  376. echo ""  
  377. echo "密码过期检查"  
  378. echo "------------"  
  379. result=""  
  380. for shell in $(grep -v "/sbin/nologin" /etc/shells);do  
  381. for user in $(echo "$pwdfile" | grep "$shell" | cut -d: -f1);do  
  382. get_expiry_date=$(/usr/bin/chage -l $user | grep 'Password expires' | cut -d: -f2) 
  383. if [[ $get_expiry_date = ' never' || $get_expiry_date = 'never' ]];then  
  384. printf "%-15s 永不过期\n" $user  
  385. result="$result,$user:never"  
  386. else  
  387. password_expiry_date=$(date -d "$get_expiry_date" "+%s")  
  388. current_date=$(date "+%s") 
  389. diff=$(($password_expiry_date-$current_date))  
  390. let DAYS=$(($diff/(60*60*24)))  
  391. printf "%-15s %s天后过期\n" $user $DAYS  
  392. result="$result,$user:$DAYS days"  
  393. fi  
  394. done  
  395. done  
  396. report_PasswordExpiry=$(echo $result | sed 's/^,//') 
  397. echo ""  
  398. echo "密码策略检查"  
  399. echo "------------"  
  400. grep -v "#" /etc/login.defs | grep -E "PASS_MAX_DAYS|PASS_MIN_DAYS|PASS_MIN_LEN|PASS_WARN_AGE" 
  401. function getSudoersStatus(){  
  402. echo ""  
  403. echo ""  
  404. echo "############################ Sudoers检查 #########################"  
  405. conf=$(grep -v "^#" /etc/sudoers| grep -v "^Defaults" | sed '/^$/d')  
  406. echo "$conf"  
  407. echo ""  
  408. #报表信息  
  409. report_Sudoers="$(echo $conf | wc -l)"  
  410. }  
  411. function getInstalledStatus(){  
  412. echo ""  
  413. echo ""  
  414. echo "############################ 软件检查 ############################"  
  415. rpm -qa --last | head | column -t   
  416. function getProcessStatus(){  
  417. echo ""  
  418. echo ""  
  419. echo "############################ 进程检查 ############################"  
  420. if [ $(ps -ef | grep defunct | grep -v grep | wc -l) -ge 1 ];then  
  421. echo ""  
  422. echo "僵尸进程";  
  423. echo "--------"  
  424. ps -ef | head -n1  
  425. ps -ef | grep defunct | grep -v grep  
  426. fi  
  427. echo ""  
  428. echo "内存占用TOP10"  
  429. echo "-------------"  
  430. echo -e "PID %MEM RSS COMMAND  
  431. $(ps aux | awk '{print $2, $4, $6, $11}' | sort -k3rn | head -n 10 )"| column -t   
  432. echo ""  
  433. echo "CPU占用TOP10"  
  434. echo "------------"  
  435. top b -n1 | head -17 | tail -11  
  436. #报表信息  
  437. report_DefunctProsess="$(ps -ef | grep defunct | grep -v grep|wc -l)"  
  438. function getJDKStatus(){  
  439. echo ""  
  440. echo ""  
  441. echo "############################ JDK检查 #############################"  
  442. java -version 2>/dev/null  
  443. if [ $? -eq 0 ];then  
  444. java -version 2>&1  
  445. fi  
  446. echo "JAVA_HOME=\"$JAVA_HOME\""  
  447. #报表信息  
  448. report_JDK="$(java -version 2>&1 | grep version | awk '{print $1,$3}' | tr -d '"')"  
  449. }  
  450. function getSyslogStatus(){  
  451. echo ""  
  452. echo ""  
  453. echo "############################ syslog检查 ##########################"  
  454. echo "服务状态:$(getState rsyslog)"  
  455. echo ""  
  456. echo "/etc/rsyslog.conf"  
  457. echo "-----------------"  
  458. cat /etc/rsyslog.conf 2>/dev/null | grep -v "^#" | grep -v "^\\$" | sed '/^$/d' | column -t  
  459. #报表信息  
  460. report_Syslog="$(getState rsyslog)"  
  461. }  
  462. function getFirewallStatus(){  
  463. echo ""  
  464. echo ""  
  465. echo "############################ 防火墙检查 ##########################"  
  466. #防火墙状态,策略等  
  467. if [[ $centosVersion < 7 ]];then  
  468. /etc/init.d/iptables status >/dev/null 2>&1 
  469. status=$?  
  470. if [ $status -eq 0 ];then  
  471. s="active"  
  472. elif [ $status -eq 3 ];then  
  473. s="inactive"  
  474. elif [ $status -eq 4 ];then  
  475. s="permission denied"  
  476. else  
  477. s="unknown"  
  478. fi  
  479. else  
  480. s="$(getState iptables)"  
  481. fi  
  482. echo "iptables: $s"  
  483. echo ""  
  484. echo "/etc/sysconfig/iptables"  
  485. echo "-----------------------"  
  486. cat /etc/sysconfig/iptables 2>/dev/null  
  487. #报表信息  
  488. report_Firewall="$s"  
  489. }  
  490. function getSNMPStatus(){  
  491. #SNMP服务状态,配置等  
  492. echo ""  
  493. echo ""  
  494. echo "############################ SNMP检查 ############################"  
  495. status="$(getState snmpd)"  
  496. echo "服务状态:$status"  
  497. echo ""  
  498. if [ -e /etc/snmp/snmpd.conf ];then  
  499. echo "/etc/snmp/snmpd.conf"  
  500. echo "--------------------"  
  501. cat /etc/snmp/snmpd.conf 2>/dev/null | grep -v "^#" | sed '/^$/d'  
  502. fi  
  503. #报表信息  
  504. report_SNMP="$(getState snmpd)"  
  505. }  
  506. function getState(){  
  507. if [[ $centosVersion < 7 ]];then  
  508. if [ -e "/etc/init.d/$1" ];then  
  509. if [ `/etc/init.d/$1 status 2>/dev/null | grep -E "is running|正在运行" | wc -l` -ge 1 ];then  
  510. r="active"  
  511. else  
  512. r="inactive"  
  513. fi  
  514. else  
  515. r="unknown"  
  516. fi  
  517. else  
  518. #CentOS 7+  
  519. r="$(systemctl is-active $1 2>&1)"  
  520. fi  
  521. echo "$r"  
  522. }  
  523. function getSSHStatus(){  
  524. #SSHD服务状态,配置,受信任主机等  
  525. echo ""  
  526. echo ""  
  527. echo "############################ SSH检查 #############################"  
  528. #检查受信任主机  
  529. pwdfile="$(cat /etc/passwd)"  
  530. echo "服务状态:$(getState sshd)"  
  531. Protocol_Version=$(cat /etc/ssh/sshd_config | grep Protocol | awk '{print $2}')  
  532. echo "SSH协议版本:$Protocol_Version"  
  533. echo "" 
  534. echo "信任主机"  
  535. echo "--------"  
  536. authorized=0  
  537. for user in $(echo "$pwdfile" | grep /bin/bash | awk -F: '{print $1}');do  
  538. authorize_file=$(echo "$pwdfile" | grep -w $user | awk -F: '{printf $6"/.ssh/authorized_keys"}')  
  539. authorized_host=$(cat $authorize_file 2>/dev/null | awk '{print $3}' | tr '\n' ',' | sed 's/,$//')  
  540. if [ ! -z $authorized_host ];then  
  541. echo "$user 授权 \"$authorized_host\" 无密码访问"  
  542. fi  
  543. let authorizedauthorized=authorized+$(cat $authorize_file 2>/dev/null | awk '{print $3}'|wc -l)  
  544. done 
  545. echo ""  
  546. echo "是否允许ROOT远程登录"  
  547. echo "--------------------"  
  548. config=$(cat /etc/ssh/sshd_config | grep PermitRootLogin)  
  549. firstChar=${config:0:1}  
  550. if [ $firstChar == "#" ];then  
  551. PermitRootLogin="yes" #默认是允许ROOT远程登录的  
  552. else  
  553. PermitRootLogin=$(echo $config | awk '{print $2}')  
  554. fi  
  555. echo "PermitRootLogin $PermitRootLogin"  
  556. echo ""  
  557. echo "/etc/ssh/sshd_config"  
  558. echo "--------------------"  
  559. cat /etc/ssh/sshd_config | grep -v "^#" | sed '/^$/d'  
  560. #报表信息  
  561. report_SSHAuthorized="$authorized" #SSH信任主机  
  562. report_SSHDProtocolVersion="$Protocol_Version" #SSH协议版本  
  563. report_SSHDPermitRootLogin="$PermitRootLogin" #允许root远程登录 
  564. }  
  565. function getNTPStatus(){ 
  566. #NTP服务状态,当前时间,配置等  
  567. echo ""  
  568. echo ""  
  569. echo "############################ NTP检查 #############################"  
  570. if [ -e /etc/ntp.conf ];then  
  571. echo "服务状态:$(getState ntpd)"  
  572. echo ""  
  573. echo "/etc/ntp.conf"  
  574. echo "-------------"  
  575. cat /etc/ntp.conf 2>/dev/null | grep -v "^#" | sed '/^$/d'  
  576. fi  
  577. #报表信息  
  578. report_NTP="$(getState ntpd)"  
  579. }  
  580. function uploadHostDailyCheckReport(){  
  581. json="{  
  582. \"DateTime\":\"$report_DateTime\",  
  583. \"Hostname\":\"$report_Hostname\",  
  584. \"OSRelease\":\"$report_OSRelease\",  
  585. \"Kernel\":\"$report_Kernel\",  
  586. \"Language\":\"$report_Language\",  
  587. \"LastReboot\":\"$report_LastReboot\",  
  588. \"Uptime\":\"$report_Uptime\",  
  589. \"CPUs\":\"$report_CPUs\",  
  590. \"CPUType\":\"$report_CPUType\",  
  591. \"Arch\":\"$report_Arch\",  
  592. \"MemTotal\":\"$report_MemTotal\",  
  593. \"MemFree\":\"$report_MemFree\",  
  594. \"MemUsedPercent\":\"$report_MemUsedPercent\",  
  595. \"DiskTotal\":\"$report_DiskTotal\",  
  596. \"DiskFree\":\"$report_DiskFree\",  
  597. \"DiskUsedPercent\":\"$report_DiskUsedPercent\", 
  598. \"InodeTotal\":\"$report_InodeTotal\",  
  599. \"InodeFree\":\"$report_InodeFree\",  
  600. \"InodeUsedPercent\":\"$report_InodeUsedPercent\",  
  601. \"IP\":\"$report_IP\",  
  602. \"MAC\":\"$report_MAC\",  
  603. \"Gateway\":\"$report_Gateway\",  
  604. \"DNS\":\"$report_DNS\",  
  605. \"Listen\":\"$report_Listen\",
  606. \"Selinux\":\"$report_Selinux\",  
  607. \"Firewall\":\"$report_Firewall\",  
  608. \"USERs\":\"$report_USERs\",  
  609. \"USEREmptyPassword\":\"$report_USEREmptyPassword\",  
  610. \"USERTheSameUID\":\"$report_USERTheSameUID\",  
  611. \"PasswordExpiry\":\"$report_PasswordExpiry\",  
  612. \"RootUser\":\"$report_RootUser\",  
  613. \"Sudoers\":\"$report_Sudoers\",  
  614. \"SSHAuthorized\":\"$report_SSHAuthorized\",  
  615. \"SSHDProtocolVersion\":\"$report_SSHDProtocolVersion\",  
  616. \"SSHDPermitRootLogin\":\"$report_SSHDPermitRootLogin\",  
  617. \"DefunctProsess\":\"$report_DefunctProsess\",  
  618. \"SelfInitiatedService\":\"$report_SelfInitiatedService\",  
  619. \"SelfInitiatedProgram\":\"$report_SelfInitiatedProgram\",  
  620. \"RuningService\":\"$report_RuningService\",  
  621. \"Crontab\":\"$report_Crontab\",  
  622. \"Syslog\":\"$report_Syslog\",  
  623. \"SNMP\":\"$report_SNMP\",  
  624. \"NTP\":\"$report_NTP\",  
  625. \"JDK\":\"$report_JDK\"  
  626. }"  
  627. #echo "$json"   
  628. curl -l -H "Content-type: application/json" -X POST -d "$json" "$uploadHostDailyCheckReportApi" 2>/dev/null  
  629. function getchage_file_24h()  
  630. echo "############################ 文件检查 #############################"  
  631.     check2=$(find / -name '*.sh' -mtime -1)  
  632. check21=$(find / -name '*.asp' -mtime -1)  
  633. check22=$(find / -name '*.php' -mtime -1)  
  634. check23=$(find / -name '*.aspx' -mtime -1)  
  635. check24=$(find / -name '*.jsp' -mtime -1)  
  636. check25=$(find / -name '*.html' -mtime -1)  
  637. check26=$(find / -name '*.htm' -mtime -1)  
  638. check9=$(find / -name core -exec ls -l {} \;)  
  639. check10=$(cat /etc/crontab)  
  640. check12=$(ls -alt /usr/bin | head -10)  
  641. cat <<EOF  
  642. ############################查看所有被修改过的文件返回最近24小时内的############################  
  643. ${check2}  
  644. ${check21}  
  645. ${check22}  
  646. ${check23}  
  647. ${check24}  
  648. ${check25}  
  649. ${check26}  
  650. ${line}  
  651. ############################检查定时文件的完整性############################  
  652. ${check10}  
  653. ${line}  
  654. ############################查看系统命令是否被替换############################  
  655. ${check12}  
  656. ${line}  
  657. EOF  
  658. }  
  659. function check(){  
  660. version  
  661. getSystemStatus  
  662. getCpuStatus  
  663. getMemStatus  
  664. getDiskStatus  
  665. getNetworkStatus  
  666. getListenStatus  
  667. getProcessStatus  
  668. getServiceStatus  
  669. getAutoStartStatus  
  670. getLoginStatus  
  671. getCronStatus  
  672. getUserStatus  
  673. getPasswordStatus  
  674. getSudoersStatus  
  675. getJDKStatus  
  676. getFirewallStatus  
  677. getSSHStatus  
  678. getSyslogStatus  
  679. getSNMPStatus  
  680. getNTPStatus  
  681. getInstalledStatus  
  682. getchage_file_24h  
  683. }  
  684. #执行检查并保存检查结果  
  685. check > $RESULTFILE  
  686. echo "检查结果:$RESULTFILE" 
  687. echo -e "`date "+%Y-%m-%d %H:%M:%S"` 阿里云PHP企业平台巡检报告"  | mail -a $RESULTFILE -s "阿里云PHP企业平台巡检报告" h@163.com  
  688. END  

相关内容