1. #!/bin/bash -
    2. type mysql > /dev/null
    3. if [ $? -eq 1 ];then
    4. VERSION_ID=`rpm -q centos-release|cut -d- -f3`
    5. if [ ${VERSION_ID} -eq 6 ];then
    6. rpm -ivh http://repo.mysql.com/mysql57-community-release-el6-11.noarch.rpm
    7. yum install -y mysql-community-common mysql-community-libs mysql-community-libs-compat postfix
    8. rpm -ivh https://repo.mysql.com/yum/mysql-5.7-community/el/6/x86_64/mysql-community-client-5.7.29-1.el6.x86_64.rpm
    9. elif [ ${VERSION_ID} -eq 7 ];then
    10. rpm -ivh http://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm
    11. yum install -y mysql-community-common mysql-community-libs mysql-community-libs-compat postfix
    12. rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.29-1.el7.x86_64.rpm
    13. else
    14. echo "Please install mysql-client first!"
    15. exit
    16. fi
    17. fi
    18. type mysql > /dev/null
    19. if [ $? -eq 1 ];then
    20. echo "Please install mysql-client first!"
    21. exit
    22. fi
    23. HOSTNAME=`hostname`
    24. DB_HOST="ip"
    25. DB_USER="user"
    26. export MYSQL_PWD="pasword"
    27. DB_NAME="by_monitor"
    28. MYSQL_CONN="mysql -h${DB_HOST} -u${DB_USER} ${DB_NAME} -N -e"
    29. BY_SERVICE=`${MYSQL_CONN} 'select service_name from by_service;'` > /dev/null 2>&1
    30. DEPLOY_DIR="/opt/www"
    31. for PID in `ps -ef|grep java|grep -v "grep"|awk '{print $2}'`
    32. do
    33. SERVICE=`pwdx ${PID}|grep "${deploy_dir}"|grep -v "grep"|awk '{print $2}'|awk -F'/' '{print $4}'`
    34. echo ${BY_SERVICE}|tr ' ' '\n'|grep -x ${SERVICE} > /dev/null 2>&1
    35. if [ $? -eq 0 ];then
    36. main_jar="${DEPLOY_DIR}/${SERVICE}/work/lib/${SERVICE}-3.0.0.jar"
    37. if [ -f ${main_jar} ];then
    38. main_jar_md5=`md5sum ${main_jar}|cut -d' ' -f1`
    39. main_jar_mtime=`stat ${main_jar}|grep "Modify"|awk -F'[ .]' '{print $2" "$3}'`
    40. else
    41. main_jar_md5=""
    42. main_jar_mtime=""
    43. fi
    44. #ps -o pid,%cpu,rsz,etime -p ${PID}
    45. service_mem=`ps -o rsz -p ${PID}|tail -n 1`
    46. service_cpu=`ps -o pcpu -p ${PID}|tail -n 1|cut -d' ' -f2`
    47. service_elapsed=`ps -o etime -p ${PID}|tail -n 1|awk '{print $1}'`
    48. service_start=`ps -o lstart -p ${PID}|tail -n 1|xargs -I {} date -d"{}" +'%F %T'`
    49. ${MYSQL_CONN} "replace into by_running(host_name,service_name,service_cpu,service_mem,start_time,update_time,service_elapsed,main_jar_md5,main_jar_mtime) values(\"${HOSTNAME}\",\"${SERVICE}\",${service_cpu},${service_mem},\"${service_start}\",now(),\"${service_elapsed}\",\"${main_jar_md5}\",\"${main_jar_mtime}\");"
    50. fi
    51. done