【Linux巡检日志】给Linux服务器添加定时巡检日志

问题描述:Linux服务器中,想在某个时间定时生成巡检日志,如:想看该服务器中哪些用户登录了,定时获取系统网络信息再或者CPU占用率,那么就用到了crontab定时命令了,在命令窗口输入crontab -l,查看Linux服务器中存在的定时任务,
crontab -e:就是编辑自己的定时任务,点击i——然后添加自己需要自动执行的巡检日志脚本的路径和定时生成的时间。
添加格式:
* * * * * 脚本的绝对路径,代表的含义 

分钟 小时 日期 月份 星期几 脚本文件
20     12      *       *        *       脚本的绝对路径  代表每天12点20分执行该脚本一次

巡检脚本checklinux.sh包含信息:
系统网络信息/服务信息/运行的服务/监听脚本/系统登陆用户/CPU占用top10/内存占用top10/用户的登录信息/查看最近24小时内所有被修改过的文件/网络连接信息查看/查看xx开头的进程等等

例如checklinux.sh:要下载文件可见:https://download.csdn.net/my/uploads/1/1

#!/bin/bash
# auth:kaliarch
# func:sys info check
# version:v1.0
# sys:centos6.x/7.x

#[ $(id -u) -gt 0 ] && echo "用户执行此脚本!"  && exit 0
sysversion=$(rpm -q centos-release|cut -d- -f3)
line="-------------------------------------------------"

[ -d logs ] || mkdir logs

sys_check_file="logs/$(ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')`date +%Y%m%d%H%m%s`.txt"

# 获取系统网络信息
function get_net_info() {
    pri_ipadd=$(ip a show dev eth0|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}')
    pub_ipadd=$(curl ifconfig.me -s)
    gateway=$(ip route | grep default | awk '{print $3}')
    mac_info=$(ip link| egrep -v "lo"|grep link|awk '{print $2}')
    dns_config=$(egrep -v "^$|^#" /etc/resolv.conf)
    route_info=$(route -n)
cat <<EOF | column -t 
IP信息:

系统公网地址: ${pub_ipadd}
系统私网地址: ${pri_ipadd}
网关地址: ${gateway}
MAC地址: ${mac_info}

路由信息:
${route_info}

DNS 信息:
${dns_config}
EOF
}


# 获取服务信息
function get_service_info() {
    port_listen=$(netstat -lntup|grep -v "Active Internet")
    if [[ ${sysversion} -gt 6 ]];then
        service_config=$(systemctl list-unit-files --type=service --state=enabled|grep "enabled")
        run_service=$(systemctl list-units --type=service --state=running |grep ".service")
    else
        service_config=$(/sbin/chkconfig | grep -E ":on|:启用" |column -t)
        run_service=$(/sbin/service --status-all|grep -E "running")
    fi
cat <<EOF
服务启动配置:

${service_config}
${line}
运行的服务:

${run_service}
${line}
监听端口:

${port_listen}
${line}
EOF
}

function get_sys_user() {
    login_user=$(awk -F: '{if ($NF=="/bin/bash") print $0}' /etc/passwd)
    
cat <<EOF
系统登录用户:

${login_user}
${line}


EOF
}

function process_top_info() {

    top_title=$(top -b n1|head -7|tail -1)
    cpu_top10=$(top b -n1 | head -17 | tail -10)
    mem_top10=$(top -b n1|head -17|tail -10|sort -k10 -r)

cat <<EOF
CPU占用top10:

${top_title}
${cpu_top10}

内存占用top10:

${top_title}
${mem_top10}
EOF
}

function get_check_info() {
	check1=$(cat /etc/passwd)	
    check2=$(find / -name '*.sh' -mtime -1)	
	check21=$(find / -name '*.asp' -mtime -1)	
	check22=$(find / -name '*.php' -mtime -1)	
	check23=$(find / -name '*.aspx' -mtime -1)
	check24=$(find / -name '*.jsp' -mtime -1)		
	check25=$(find / -name '*.html' -mtime -1)
	check26=$(find / -name '*.htm' -mtime -1)	
	check3=$(netstat -antlp | grep "ESTABLISHED" |more)
    check4=$(ps -aef | grep inetd)
    check5=$(ps -aef)
	check7=$(last -20)
	check8=$(ls -al /var/log)
	check9=$(find / -name core -exec ls -l {} \;)
	check9=$(crontab -l)
	check10=$(cat /etc/crontab)
	check11=$(ls -alt /etc/init.d/)
	check12=$(ls -alt /usr/bin | head -10)
	check13=$(ls -alt /tmp/)
	check15=$(last -f /var/log/wtmp)
	check16=$(last -f /var/run/utmp)
cat <<EOF

用户的登录信息:

${check1}
${line}

查看所有被修改过的文件返回最近24小时内的
${check2}
${check21}
${check22}
${check23}
${check24}
${check25}
${check26}
${line}

网络连接信息查看:

${check3}
${line}

查看inetd进程,主要查看inetd -s参数:

${check4}
${line}

查看./xxx开头的进程:
${check5}
${line}

查询用户的历史记录(只查询最近的20条记录),观察syslog进程情况:
${check7}
${line}

检查wtmp utmp,包括messgae等文件:

${check8}
${line}

检查定时任务:

${check9}
${line}


检查定时文件的完整性:
${check10}
${line}


开机启动程序:

${check11}
${line}

查看系统命令是否被替换:

${check12}
${line}

查看敏感目录的文件分析:

${check13}
${line}

登录过本系统的用户的信息:

${check15}
${line}

正在本系统中的用户的信息:

${check16}
${line}

EOF
}
	
function sys_check() {
    get_check_info
	echo ${line}		
    get_net_info
    echo ${line}
    get_service_info
    echo ${line}
    get_sys_user
    echo ${line}
    process_top_info
	
}

sys_check > ${sys_check_file}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值