{"id":254580,"date":"2022-10-16T09:07:05","date_gmt":"2022-10-16T01:07:05","guid":{"rendered":"https:\/\/gulass.cn\/?p=254580"},"modified":"2022-10-13T09:07:23","modified_gmt":"2022-10-13T01:07:23","slug":"dummy-pampy-elementui","status":"publish","type":"post","link":"https:\/\/gulass.cn\/dummy-pampy-elementui.html","title":{"rendered":"Linux\u6027\u80fd\u8fd0\u7ef4–\u4e09\u4e2aPython\u5c0f\u5de5\u5177"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u7531\u4e8e\u5de5\u4f5c\u9700\u8981\uff0c\u670d\u52a1\u5668\u6bcf\u5468\u9700\u5de1\u68c0\u4e00\u6b21\u3002\u9664\u4e86\u5185\u5b58\u3001cpu\u3001\u78c1\u76d8\u3001\u7f51\u7edc\u8fde\u63a5\u7b49\u6570\u636e\u5916\uff0c\u8fd8\u6709\u4e0d\u540c\u7684\u670d\u52a1\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4e3a\u52a0\u5feb\u5de1\u68c0\u901f\u5ea6\u548c\u77e5\u6653\u670d\u52a1\u5668\u72b6\u51b5\uff0c\u6211\u4f7f\u7528\u4e86multiprocessing.dummy\u591a\u7ebf\u7a0b\u3001pampy\u6a21\u5f0f\u5339\u914d\u548c\u4e00\u4e2aelementUI\u7ec4\u4ef6\uff0c\u8fd9\u4e9b\u5de5\u5177\u786e\u80fd\u5e2e\u5fd9\u5904\u7406\u5b9e\u9645\u95ee\u9898\uff1a<\/p>\n

    \n
  • \u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u5e76\u884c<\/li>\n<\/ul>\n
      \n
    • \u539f\u672c\u4e8c\u5341\u591a\u53f0\u670d\u52a1\u5668\u5de1\u68c0\u7528shell\u97003\u5206\u949f\u5b8c\u6210\uff0c\u7528multiprocessing.dummy\u6a21\u5757\u540e\u53ea\u9700\u5341\u51e0\u79d2<\/li>\n<\/ul>\n
        \n
      • pampy\u6a21\u5f0f\u5339\u914d<\/li>\n<\/ul>\n

        \u642d\u914d\u6b63\u5219\uff0c\u5339\u914d\u51fa2\u5757\u78c1\u76d8\u7684\u4f7f\u7528\u7387\uff0c\u65b9\u4fbf\u524d\u7aef\u9875\u9762\u5c55\u793a\u3002\u6570\u636e\u662f\u4ece\u771f\u5b9e\u670d\u52a1\u5668\u4e0a\u83b7\u53d6\u7684\u3002<\/p>\n

        import re\r\nfrom pampy import match\r\ndisk_usage = 'diskname: \/dev\/vda1 used: 35G nouse: 59G USAGE: 35.35% diskname: \/dev\/vdb1 used: 278G nouse: 190G USAGE: 56.39%'\r\nre_regular = re.compile('diskname: (\/\\\\w+\/\\\\w+).*USAGE: (.*)% diskname: (\/\\\\w+\/\\\\w+).*USAGE: (.*)%')\r\nm= match(disk_usage, re_regular, lambda a,b,c,d: b+','+d)\r\nprint(m)<\/pre>\n

        \u7ed3\u679c\u5982\u4e0b\uff1a35.35,56.39<\/p>\n

        \u5339\u914d\u597d\u540e\uff0c\u8fd92\u4e2a\u503c\u5f88\u65b9\u4fbf\u5c31\u80fd\u5728\u524d\u7aef\u9875\u9762\u8fdb\u884c\u5c55\u793a\u4e86<\/p>\n

        \"\"<\/p>\n

        \u8fd9\u662felementUI\u5b98\u7f51\u7684\u8fdb\u5ea6\u6761\u7ec4\u4ef6<\/p>\n

        \"\"<\/p>\n

        \u8fd9\u662f\u4f7f\u7528\u5b83\u7684\u6548\u679c\uff1a<\/p>\n

        \"\"<\/p>\n

        \u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd93\u70b9\uff1a<\/strong><\/div>\n
        1. \u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u5e76\u884c<\/strong><\/span><\/div>\n

        \u5728\u67d0\u53f0\u670d\u52a1\u5668\u4e0a\u505a\u514d\u5bc6\u94a5\u767b\u5f55\uff0c\u80fd\u76f4\u8fde\u5230\u5176\u5b83\u670d\u52a1\u5668\u6267\u884cshell\u811a\u672c\uff0c\u4e4b\u524d\u7684\u5de1\u68c0\u662f\u901a\u8fc7shell\u811a\u672c\u6267\u884c\u7684\uff1a<\/p>\n

        #\u767b\u5f55\u4e0d\u540c\u7684ip\uff0c\u4f9d\u6b21\u6267\u884c\/home\/ssw\/\u76ee\u5f55\u4e0b\u7684\u68c0\u67e5\u811a\u672c\r\nfor ip in `cat \/home\/ssw\/iplist`;do ssh user@$ip \"\/bin\/sh \/home\/ssw\/weekly_check.sh\";done<\/pre>\n

        \u56e0\u4e3a\u662f\u4e32\u884c\u6267\u884c\uff0c\u7ecf\u5e38\u7b49\u5230\u82b1\u513f\u90fd\u8c22\u4e86\u3002\u4e8e\u662f\u6539\u7528python\u53bb\u6267\u884c\u8fd9\u4e9bshell\u547d\u4ee4\uff1acpu\u3001\u5185\u5b58\u3001\u78c1\u76d8\u7684\u68c0\u67e5\u547d\u4ee4\u90fd\u4e00\u6837\uff0c\u4e0d\u540c\u7684\u670d\u52a1\u53ea\u9700\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\uff0c\u6839\u636eip\u6dfb\u52a0\u76f8\u5173\u547d\u4ee4\u3002\u518dpool.map()\u4e00\u884c\u5b9e\u73b0\u591a\u7ebf\u7a0b\u3002<\/p>\n

        # -*- coding: utf-8 -*-\r\nimport paramiko\r\nimport json\r\nfrom datetime import datetime\r\nimport traceback\r\nfrom pprint import pprint\r\nfrom multiprocessing.dummy import Pool as ThreadPool\r\ndef weekly_check(ip):\r\n#\u57fa\u7840\u5de1\u68c0\u6307\u6807\r\ncmds_dict = {'cpu_usage':'TIME_INTERVAL=5;LAST_CPU_INFO=$(cat \/proc\/stat | grep -w cpu | awk \\'{print $2,$3,$4,$5,$6,$7,$8}\\');LAST_SYS_IDLE=$(echo $LAST_CPU_INFO | awk \\'{print $4}\\');LAST_TOTAL_CPU_T=$(echo $LAST_CPU_INFO | awk \\'{print $1+$2+$3+$4+$5+$6+$7}\\');sleep ${TIME_INTERVAL};NEXT_CPU_INFO=$(cat \/proc\/stat | grep -w cpu | awk \\'{print $2,$3,$4,$5,$6,$7,$8}\\');NEXT_SYS_IDLE=$(echo $NEXT_CPU_INFO | awk \\'{print $4}\\');NEXT_TOTAL_CPU_T=$(echo $NEXT_CPU_INFO | awk \\'{print $1+$2+$3+$4+$5+$6+$7}\\');SYSTEM_IDLE=`echo ${NEXT_SYS_IDLE} ${LAST_SYS_IDLE} | awk \\'{print $1-$2}\\'`;TOTAL_TIME=`echo ${NEXT_TOTAL_CPU_T} ${LAST_TOTAL_CPU_T} | awk \\'{print $1-$2}\\'`;CPU_USAGE=`echo ${SYSTEM_IDLE} ${TOTAL_TIME} | awk \\'{printf \"%.2f\", 100-$1\/$2*100}\\'`;echo ${CPU_USAGE}',\r\n'mem_usage':'MEM_USAGE=`\/usr\/bin\/free | awk \\'\/Mem\/{printf(\"RAM Usage: %.2f%\\\\n\"), $3\/$2*100}\\' | awk \\'{print $3}\\'`;echo ${MEM_USAGE}',\r\n'disk_status':'DISK_STATUS=`df -h | grep \"^\/dev\/vd\" | awk \\'{printf \"diskname: %-10s used: %-5s nouse: %-5s USAGE: %.2f%\\\\n\",$1,$3,$4,$3\/$2*100}\\'`;echo ${DISK_STATUS}',\r\n'network': 'if ping -c 5 www.baidu.com &>\/dev\/null;then echo \"Network: OK\";else echo \"Network: NOT OK\";fi',\r\n'boot_log': 'B=`cat \/var\/log\/boot.log`;if [ \"$B\" = \"\" ];then echo \"Bootlog: OK\";else echo\"Bootlog: NOT OK\";fi',\r\n}\r\nif ip == '172.16.1.21':\r\ncmds_dict['mysql'] = 'mysql_pid=`ps -ef | grep mysql | grep -v grep | awk \\'{print $2}\\'`;if [ \"${mysql_pid}\" = \"\" ];then echo \"Mysql_service: NOT OK\";else echo \"Mysql_service: OK! pid is ${mysql_pid}\";fi'\r\nelif ip == '172.16.1.22':\r\ncmds_dict['es'] = 'es_pid=`ps -ef | grep elasticsearch | grep -v grep | awk \\'{print $2}\\'`;if [ \"${es_pid}\" = \"\" ];then echo \"Es_service: NOT OK\";else echo \"Es_service: OK pid is ${es_pid}\";fi'\r\nelif ip == '172.16.1.23':\r\ncmds_dict['redis_cluster'] = 'NUM=` ps -ef | grep redis | grep -v grep |awk \\'{print $2}\\' | wc -l`;echo \"the running redis-cluster node is $NUM\"'\r\ntry:\r\n#\u521b\u5efassh\u5ba2\u6237\u7aef\r\nclient = paramiko.SSHClient()\r\n#\u514d\u5bc6\u94a5\u767b\u5f55\r\nprivate_key = paramiko.RSAKey.from_private_key_file('\/home\/ssw\/.ssh\/id_rsa')\r\nclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())\r\nclient.connect(\r\nhostname=ip,\r\nusername='ssw',\r\nport=22,\r\npkey=private_key,\r\ntimeout=30\r\n)\r\n#\u65b0\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\u5b58\u50a8\u8f93\u51fa\u7ed3\u679c\r\nresult = {}\r\nfor k,v in cmds_dict.items():\r\nstdin, stdout, stderr = client.exec_command(v)\r\nif not stderr.read():\r\nresult['ip'] = ip\r\nresult[k] = stdout.read().decode('utf-8').strip()\r\nelse:\r\npass\r\nexcept Exception as e:\r\npprint(ip+\" error:\"+str(e))\r\npprint(traceback.format_exc())\r\nfinally:\r\nclient.close()\r\nreturn result\r\nif __name__ == '__main__':\r\nip_list = ['172.16.1.21','172.16.1.22','172.16.1.23']\r\npool = ThreadPool(8)\r\nret = pool.map(weekly_check,ip_list)\r\npool.close()\r\npool.join()\r\n#\u7ed3\u679c\u5199\u5165excel\r\nwith open('\/home\/ssw\/game_server_%s.xlsx' % datetime.now().__format__('%m-%d'),'w') as f:\r\nf.write(json.dumps(ret))<\/pre>\n

        \u6709\u4e9b\u670d\u52a1\u5668\u67092\u5757\u78c1\u76d8,\u6709\u4e9b\u53ea\u67091\u5757\uff0c\u8fd9\u662f\u6267\u884c\u811a\u672c\u540e\u8f93\u51fa\u7684\u90e8\u5206\u6570\u636e\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

        [{'cpu_usage': '11.67',\r\n'disk_status': 'diskname: \/dev\/vda1 used: 35G nouse: 59G USAGE: 35.35% '\r\n'diskname: \/dev\/vdb1 used: 243G nouse: 225G USAGE: 49.29%',\r\n'es': 'Es_service: OK pid is 20488',\r\n'ip': '172.16.1.21',\r\n'mem_usage': '27.13%',\r\n'network': 'Network: OK'},\r\n{'cpu_usage': '3.14',\r\n'disk_status': 'diskname: \/dev\/vda1 used: 23G nouse: 445G USAGE: 4.67%',\r\n'ip': '172.16.1.22',\r\n'mem_usage': '12.86%',\r\n'network': 'Network: OK',\r\n'rabbitmq': 'Rabbitmq_service: OK pid is 1392'},\r\n]<\/pre>\n
        2. \u6570\u636e\u5199\u5165mysql\uff08\u7528\u5230pampy\uff09<\/strong><\/span><\/div>\n

        1\uff09\u521b\u5efa\u6570\u636e\u5e93<\/p>\n

        create table weekly_check\r\n(\r\nid int not null auto_increment,\r\nproject varchar(30),\r\nip varchar(30),\r\ncpu varchar(30),\r\nmem varchar(30),\r\ndisk LONGTEXT,\r\nnetwork varchar(60),\r\nvda1 varchar(20),\r\nvdb1 varchar(20),\r\nservice LONGTEXT,\r\ncreate_time timestamp null default current_timestamp,\r\nprimary key (id)\r\n);<\/pre>\n

        2\uff09\u5199\u5165mysql<\/p>\n

        pampy\u5728\u8fd9\u91cc\u7684\u4f5c\u7528\uff0c\u4e3b\u8981\u662f\u627e\u51fa2\u5757\u78c1\u76d8\u7684\u4f7f\u7528\u7387\uff0c\u4f5c\u4e3a\u6570\u636e\u63d2\u5165\u5230\"vda1\",\"vdb1\"\u5b57\u6bb5\u3002<\/p>\n

        \u8fd9\u6837\u6570\u636e\u5e93\u5c31\u6709\u4e86\u5de1\u68c0\u6570\u636e\u3002<\/p>\n

        import json,pymysql\r\nimport re\r\nfrom pampy import match, HEAD, TAIL, _\r\n#\u524d\u9762\u5de1\u68c0\u7684\u90e8\u5206\u6570\u636e\r\ndata = [{'cpu_usage': '11.67',\r\n'disk_status': 'diskname: \/dev\/vda1 used: 35G nouse: 59G USAGE: 35.35% '\r\n'diskname: \/dev\/vdb1 used: 243G nouse: 225G USAGE: 49.29%',\r\n'es': 'Es_service: OK pid is 20488',\r\n'ip': '172.16.1.21',\r\n'mem_usage': '27.13%',\r\n'network': 'Network: OK',\r\n'service': {'boot_log': 'Bootlog: OK',\r\n'front_service': 'Front_service: OK! pid is 16608',\r\n'nodejs_service': 'nodejs_service: NOT OK'}\r\n},\r\n{'cpu_usage': '3.14',\r\n'disk_status': 'diskname: \/dev\/vda1 used: 23G nouse: 445G USAGE: 4.67%',\r\n'ip': '172.16.1.22',\r\n'mem_usage': '12.86%',\r\n'network': 'Network: OK',\r\n'rabbitmq': 'Rabbitmq_service: OK pid is 1392',\r\n'service': {'mysql': 'mysql: OK'}\r\n}\r\n]\r\ndef conn_mysql(sql,value):\r\ndbparam = {\r\n'host': '127.0.0.1',\r\n'port': 3306,\r\n'user': 'root',\r\n'password': '1024',\r\n'database': 'alerts',\r\n'charset': 'utf8'\r\n}\r\nconn = pymysql.connect(**dbparam)\r\ncursor = conn.cursor()\r\ntry:\r\ncursor.execute(sql,value)\r\nconn.commit()\r\nexcept Exception as e:\r\nprint('\u5165\u5e93\u5931\u8d25', e)\r\nconn.rollback()\r\nfinally:\r\ncursor.close()\r\nconn.close()\r\nfor info in data:\r\nsql = \"insert into weekly_check(project,ip,cpu,mem,disk,network,vda1,vdb1,service) values (%s, %s, %s, %s, %s, %s, %s, %s, %s)\"\r\ndisk_status = info['disk_status']\r\n#\u591a\u4e2a\u201c\u670d\u52a1\u201d\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u4e3e\u4e2a🌰\u201cnginx is ok\u201d\u548c\"tomcat is ok\"\r\nservice = ''\r\nfor k,v in info['service'].items():\r\n# \u591a\u4e2a\u201c\u670d\u52a1\u201d\u7528<br>\u6362\u884c\u7b26\u5206\u5272\uff0c\u524d\u7aef\u53ef\u4ee5\u8bc6\u522b\u5b83\u8fdb\u884c\u6362\u884c\r\nservice = service + v.split('pid')[0].strip() + '<br>'\r\n# \u7f51\u7edc\u8fde\u63a5\u3002\u4e3b\u8981\u662f\u4e3a\u4e86\u65b9\u4fbf\u524d\u7aef\u663e\u793a\uff0c\u5982\u679cok\u5219\u76f4\u63a5\u663e\u793a\u201c\u6b63\u5e38\u201d\r\nif info['network'] == 'Network: OK':\r\ninfo['network'] = '\u6b63\u5e38'\r\nelse:\r\ninfo['network'] = '\u5f02\u5e38'\r\n# \u78c1\u76d8\u5b57\u7b26\u4e32\u957f\u5ea6\u5927\u4e8e60\u8bf4\u660e\u67092\u5757\u78c1\u76d8\r\nif len(disk_status) > 60:\r\n#\u53d6\u51fa\u4e24\u5757\u78c1\u76d8vda1,vdb1\u7684\u4f7f\u7528\u7387\r\nre_regular = re.compile('diskname: (\/\\w+\/\\w+).*USAGE: (.*)% diskname: (\/\\w+\/\\w+).*USAGE: (.*)%')\r\nm = match(disk_status, re_regular, lambda a,b,c,d: b+','+d)\r\nvda1,vdb1 = m.split(',')\r\ndisk_status = info['disk_status']\r\n#\u53bb\u6389\u7b2c\u4e00\u4e2aUSAGE\r\ndisk_status = re.sub('USAGE: .*% ', '', disk_status)\r\n# \u53bb\u6389\u7b2c\u4e8c\u4e2aUSAGE\r\ndisk_status = re.sub(' USAGE: .*%', '', disk_status)\r\n#\u53bb\u6389\u7b2c\u4e00\u4e2adiskname\uff0c\u7b2c\u4e8c\u4e2adiskname\u66ff\u6362\u4e3a\u6362\u884c\u7b26<br>\r\ndisk_status = disk_status.replace('diskname: ', '', 1).replace(' diskname: ', '<br><br>')\r\nconn_mysql(sql, ('\u6e38\u620f',info['ip'], info['cpu_usage'], info['mem_usage'].strip('%'),\\\r\ndisk_status,info['network'],vda1,vdb1,service))\r\nelse:\r\n#\u53ea\u6709\u4e00\u5757\u78c1\u76d8vda1\r\nre_regular = re.compile('diskname: (\/\\w+\/\\w+).*USAGE: (.*)%')\r\nm = match(disk_status, re_regular, lambda a,b: b)\r\nconn_mysql(sql, ('\u6e38\u620f',info['ip'], info['cpu_usage'], info['mem_usage'].strip('%'), info['disk_status'],info['network'],m,'\u65e0',service))<\/pre>\n
        3\u3001vue\u5c55\u793a\u5185\u5b58\u767e\u5206\u6bd4<\/strong><\/span><\/div>\n

        el-progress\u7ec4\u4ef6\uff0c\u4f7f\u7528\u7387\u5c0f\u4e8e50%\u663e\u793a\u7eff\u8272\uff0c50%~75%\u663e\u793a\u6a59\u8272\u3002<\/p>\n

        <span v-if=\"item.prop === 'mem'\"><el-progress :text-inside=\"true\" :stroke-width=\"13\" \r\n:percentage=\"scope.row[item.prop]\" status=\"success\" v-if=\"scope.row[item.prop] < 50\">\r\n<\/el-progress>\r\n<\/span>\r\n<span v-if=\"item.prop === 'mem'\"><el-progress :text-inside=\"true\" :stroke-width=\"13\" \r\n:percentage=\"scope.row[item.prop]\" status=\"warning\" v-if=\"scope.row[item.prop] >= 50 && scope.row[item.prop] < 75\">\r\n<\/el-progress>\r\n<\/span><\/pre>\n

        \"\"<\/p>\n

        \u5c0f\u7ed3<\/strong><\/div>\n

        \u5f88\u591a\u6a21\u5757\u548c\u5de5\u5177\u5f00\u7bb1\u5373\u7528\uff0c\u662f\u8282\u7701\u65f6\u95f4\u7684\u5229\u5668\uff0c\u53ef\u4ee5\u7528\u5b83\u642d\u4e2a\u4fbf\u8f66\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

        \u4e3a\u52a0\u5feb\u5de1\u68c0\u901f\u5ea6\u548c\u77e5\u6653\u670d\u52a1\u5668\u72b6\u51b5\uff0c\u6211\u4f7f\u7528\u4e86multiprocessing.dummy\u591a\u7ebf\u7a0b\u3001pampy\u6a21\u5f0f\u5339\u914d\u548c […]<\/p>\n","protected":false},"author":323,"featured_media":231815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-254580","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/254580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=254580"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/254580\/revisions"}],"predecessor-version":[{"id":254587,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/254580\/revisions\/254587"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/231815"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=254580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=254580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=254580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}