关于linux环境的日常操作,我分享一点经验。
对于linux系统的日常监控运维,无非是一些内存、cpu、文件空间、进程、端口等等,一些列的运维,这些都可以通过简单的linux命令进行解决。
通过撰写shell脚本,对环境进行运维,一下简单介绍几种:
1.文件的操作
将文件放到远程机器 |
将远程文件达到本地 |
将远程文件删除 |
ftpPutFiles(){ remoteHost=$1 ftpUser=$2 ftpPwd=$3 remoteDir=$4 localDir=$5 localFiles=$6
ftp -i -v -n $remoteHost <<END_FTP user $ftpUser $ftpPwd binary lcd $localDir cd $remoteDir mput $localFiles bye END_FTP (sleep 2; echo $ftpUser; sleep 2; echo $ftpPwd; sleep 2; echo "chmod a+rwx $remoteDir/$localFiles"; sleep 2; echo "exit") | telnet $remoteHost 2>&1 | grep -v "getwinsize" } |
ftpGetFiles(){ remoteHost=$1 ftpUser=$2 ftpPwd=$3 remoteDir=$4 remoteFiles=$5 localDir=$6
ftp -i -v -n $remoteHost <<END_FTP
user $ftpUser $ftpPwd binary lcd $localDir cd $remoteDir mget $remoteFiles bye END_FTP } |
ftpDelFiles(){ remoteHost=$1 ftpUser=$2 ftpPwd=$3 remoteDir=$4 remoteFiles=$5
ftp -i -v -n $remoteHost <<END_FTP user $ftpUser $ftpPwd binary cd $remoteDir mdel $remoteFiles bye END_FTP } |
2.文件系统的检查脚本
checkFileSystem(){ REMOTE_HOST=$1 REMOTE_USER=$2 REMOTE_PWD=$3 FILE_SYSTEM_LOG=fileSystem.log (sleep 1; echo $REMOTE_USER; sleep 1; echo $REMOTE_PWD; sleep 1; echo "df -h|grep %|awk '{print $(NF-1),$NF}'|awk -F '%' '{if ($1 > 80) {print "$REMOTE_HOST",$1"%",$2}}'>$FILE_SYSTEM_LOG"; sleep 1; echo "exit") | telnet $REMOTE_HOST 2>&1 | grep -v "getwinsize" >tmp.out ftpGetFiles $REMOTE_HOST $REMOTE_USER $REMOTE_PWD /home/$REMOTE_USER $FILE_SYSTEM_LOG . >/dev/null ftpDelFiles $REMOTE_HOST $REMOTE_USER $REMOTE_PWD /home/$REMOTE_USER $FILE_SYSTEM_LOG >/dev/null cat $FILE_SYSTEM_LOG >> $LOG_FILE rm $FILE_SYSTEM_LOG }
|
3.进程端口的检查
checkRabbitMq(){ REMOTE_HOST=$1 REMOTE_USER=$2 REMOTE_PWD=$3 CHECK_MQ_SCRIPTS=checkMQ.sh echo "oldIfs=$IFS; IFS=':' processNums=\`ps -ef | grep rabbitmq | grep -v grep | wc -l \` if [ $processNums -eq 1 ]; then echo "hostname:" \`hostname\` ,"RabbitMq,process status:true" else echo "hostname:" \`hostname\`,"RabbitMq,process status:false" fi IFS=$oldIfs">>$CHECK_MQ_SCRIPTS ftpPutFiles $REMOTE_HOST $REMOTE_USER $REMOTE_PWD /home/$REMOTE_USER . $CHECK_MQ_SCRIPTS >/dev/null (sleep 1; echo $REMOTE_USER; sleep 1; echo $REMOTE_PWD; sleep 1; echo "sh $CHECK_MQ_SCRIPTS"; sleep 2; echo "rm $CHECK_MQ_SCRIPTS"; sleep 1; echo "exit") | telnet $REMOTE_HOST 2>&1 | grep -v "getwinsize" >tmp.out cat tmp.out |grep RabbitMq >>$LOG_FILE rm tmp.out }
|