提交 c03173e8 作者: martin

window版进程监控exporter

上级 57367321
2024-01-22 12:53:04 - urllib3.connectionpool - DEBUG - connectionpool:Starting new HTTP connection (1): 127.0.0.1:8760
2024-01-22 12:53:04 - urllib3.connectionpool - DEBUG - connectionpool:http://127.0.0.1:8760 "GET / HTTP/1.1" 200 5200
2024-01-22 12:53:04 - root - INFO - pjzx_service_monitor:200
2024-01-22 12:53:33 - urllib3.connectionpool - DEBUG - connectionpool:Starting new HTTP connection (1): 127.0.0.1:8760
2024-01-22 12:53:33 - urllib3.connectionpool - DEBUG - connectionpool:http://127.0.0.1:8760 "GET / HTTP/1.1" 200 5200
2024-01-22 12:53:33 - root - INFO - pjzx_service_monitor:200
2024-01-22 12:53:47 - urllib3.connectionpool - DEBUG - connectionpool:Starting new HTTP connection (1): 127.0.0.1:8760
2024-01-22 12:53:47 - urllib3.connectionpool - DEBUG - connectionpool:http://127.0.0.1:8760 "GET / HTTP/1.1" 200 5207
2024-01-22 12:53:47 - root - INFO - pjzx_service_monitor:200
2024-01-22 12:55:41 - root - INFO - pjzx_service_monitor:200
2024-01-22 12:55:57 - urllib3.connectionpool - DEBUG - connectionpool:Starting new HTTP connection (1): 127.0.0.1:8760
2024-01-22 12:55:57 - urllib3.connectionpool - DEBUG - connectionpool:http://127.0.0.1:8760 "GET / HTTP/1.1" 200 5318
2024-01-22 12:55:57 - root - INFO - pjzx_service_monitor:200
2024-01-23 14:19:28 - werkzeug - INFO - _internal:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:7777
2024-01-23 14:19:28 - werkzeug - INFO - _internal:Press CTRL+C to quit
2024-01-23 14:19:49 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:19:49] "GET / HTTP/1.1" 404 -
2024-01-23 14:19:54 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:19:54] "GET /status HTTP/1.1" 200 -
2024-01-23 14:19:55 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:19:55] "GET /status HTTP/1.1" 200 -
2024-01-23 14:19:56 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:19:56] "GET /status HTTP/1.1" 200 -
2024-01-23 14:20:09 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:20:09] "GET /status HTTP/1.1" 200 -
2024-01-23 14:20:09 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:20:09] "GET /status HTTP/1.1" 200 -
2024-01-23 14:20:10 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:20:10] "GET /status HTTP/1.1" 200 -
2024-01-23 14:20:17 - urllib3.connectionpool - DEBUG - connectionpool:Starting new HTTP connection (1): 127.0.0.1:8760
2024-01-23 14:20:35 - werkzeug - INFO - _internal:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:7777
2024-01-23 14:20:35 - werkzeug - INFO - _internal:Press CTRL+C to quit
2024-01-23 14:20:37 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:20:37] "GET /status HTTP/1.1" 200 -
2024-01-23 14:21:24 - urllib3.connectionpool - DEBUG - connectionpool:Starting new HTTP connection (1): 127.0.0.1:8760
2024-01-23 14:21:29 - werkzeug - INFO - _internal:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:7777
2024-01-23 14:21:29 - werkzeug - INFO - _internal:Press CTRL+C to quit
2024-01-23 14:21:32 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:21:32] "GET /status HTTP/1.1" 200 -
2024-01-23 14:21:34 - pjzx_service_monitor - ERROR - app:Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 1463, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 873, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 892, in finalize_request
response = self.make_response(rv)
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 1164, in make_response
raise TypeError(
TypeError: The view function for 'default' did not return a valid response. The function either returned None or ended without a return statement.
2024-01-23 14:21:34 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:21:34] "GET / HTTP/1.1" 500 -
2024-01-23 14:21:39 - pjzx_service_monitor - ERROR - app:Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 1463, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 873, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 892, in finalize_request
response = self.make_response(rv)
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 1164, in make_response
raise TypeError(
TypeError: The view function for 'default' did not return a valid response. The function either returned None or ended without a return statement.
2024-01-23 14:21:39 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:21:39] "GET / HTTP/1.1" 500 -
2024-01-23 14:21:49 - pjzx_service_monitor - ERROR - app:Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 1463, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 873, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 892, in finalize_request
response = self.make_response(rv)
File "C:\Users\EDY\AppData\Roaming\Python\Python310\site-packages\flask\app.py", line 1164, in make_response
raise TypeError(
TypeError: The view function for 'default' did not return a valid response. The function either returned None or ended without a return statement.
2024-01-23 14:21:49 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:21:49] "GET / HTTP/1.1" 500 -
2024-01-23 14:22:08 - urllib3.connectionpool - DEBUG - connectionpool:Starting new HTTP connection (1): 127.0.0.1:8760
2024-01-23 14:22:12 - werkzeug - INFO - _internal:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:7777
2024-01-23 14:22:12 - werkzeug - INFO - _internal:Press CTRL+C to quit
2024-01-23 14:22:14 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:22:14] "GET / HTTP/1.1" 200 -
2024-01-23 14:22:15 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:22:15] "GET / HTTP/1.1" 200 -
2024-01-23 14:22:16 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:22:16] "GET / HTTP/1.1" 200 -
2024-01-23 14:22:17 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:22:17] "GET / HTTP/1.1" 200 -
2024-01-23 14:22:19 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:22:19] "GET / HTTP/1.1" 200 -
2024-01-23 14:22:27 - werkzeug - INFO - _internal:127.0.0.1 - - [23/Jan/2024 14:22:27] "GET /aa HTTP/1.1" 404 -
2024-01-23 14:29:40 - urllib3.connectionpool - DEBUG - connectionpool:Starting new HTTP connection (1): 127.0.0.1:8760
# 进程判断标志,启动脚本,是否curl辅助判断服务状态,url地址,服务状态判断内容
svc_test,svc_test &,Y,127.0.0.1:7777/status,is running
mysql:
host: "114.115.172.99"
port: '3306'
user: "root"
pass: "@zzsn9988"
db : 'service_info'
import pymysql
import yaml
import flask
# 连接数据库
with open('config.yml', 'r') as file:
config = yaml.safe_load(file)
host = config['mysql']['host']
port = config['mysql']['port']
user = config['mysql']['user']
password = config['mysql']['pass']
db = config['mysql']['db']
my_db = pymysql.connect(
host=host,
port=int(port),
user=user,
password=password,
database=db
)
# 创建游标对象
cursor = my_db.cursor()
# 执行查询语句
cursor.execute("SELECT pub_ip FROM basic_info")
# 获取全部查询结果
result = cursor.fetchall()
# 打印查询结果
print(result)
print(type(result))
# for row in result:
# print(row[1])
# 关闭游标和数据库连接
cursor.close()
my_db.close()
#!/bin/bash
# 处理服务是否之前存在,存在则停止,无则不处理
if systemctl status svc_chk ;then
echo "svc_chk服务已存在,即将停止"
systemctl stop svc_chk && systemctl disable svc_chk
fi
# 设置脚本所在文件夹
svc_home=$(cd "$(dirname "$0")"||exit 1;pwd)
# 判断脚本文件是否存在
if [ -e svc_chk.sh ];then
if [ ! -x svc_chk.sh ];then
if chmod +x svc_chk.sh;then
echo "svc_chk.sh文件添加可执行权限成功"
else
echo "svc_chk.sh文件添加可执行权限失败,退出安装"
exit 1
fi
fi
else
echo "svc_chk.sh脚本文件不存在,请检查。。"
exit 1
fi
# 设置文件周期和行周期
if [[ $# -eq 0 ]];then
file_period=300
line_period=10
fi
if [[ $# -eq 1 ]];then
file_period=${1}
line_period=10
fi
if [[ $# -eq 2 ]];then
file_period=$1
line_period=$2
fi
# 创建服务
cat >/usr/lib/systemd/system/svc_chk.service<<EOF
[Unit]
Description=svc_chk service
After=network.target
[Service]
WorkingDirectory=$svc_home
ExecStart=${svc_home}/svc_chk.sh $file_period $line_period
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && systemctl enable svc_chk && systemctl restart svc_chk
systemctl status svc_chk
import requests
import logging
import flask
logging.basicConfig(
filename='1.log',
format='%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG
)
def run():
response = requests.get('http://127.0.0.1:8760')
print(response.status_code)
logging.info(response.status_code)
# return response.status_code
app = flask.Flask(__name__)
@app.route('/status')
def status():
return 'The System is running'
@app.route('/')
def default():
return status()
if __name__ == '__main__':
app.run(port=7777)
run()
#!/bin/bash
source /etc/profile
#日志格式化设置
logging_file="./service_check.log"
function log(){
echo "$(date '+%Y-%m-%d %H:%M:%S')" "$@" >>$logging_file
}
# 判断配置文件是否存在,不存在就创建
[ -e cfg.csv ] || echo "# 进程判断标志,启动脚本,是否curl辅助判断服务状态,url地址,服务状态判断内容" >cfg.txt
# 定义默认文件周期和行周期
file_period=300
line_period=10
if [[ $# -eq 0 ]];then
log "Use default period:file_period=300s,line_period=10s"
fi
if [[ $# -eq 1 ]];then
file_period=${1}
log "Use period:file_period=${file_period}s,line_period=10s"
fi
if [[ $# -eq 2 ]];then
file_period=$1
line_period=$2
log "Use period:file_period=${file_period}s,line_period=${line_period}s"
fi
function curl_judge() {
c_flag=$1
c_url=$2
c_cont=$3
c_pid=$4
c_start=$5
if [[ $c_flag == 'Y' ]];then
log "接口${c_url}需要curl检测"
if curl -s "$c_url" | grep -q "$c_cont"; then
log "接口$c_url 结果返回正常"
else
log "接口$c_url 结果返回不正常,判断为假死,重启进程"
kill -9 "$c_pid"
$c_start
fi
else
log "接口${c_url}不需要curl检测"
fi
}
trap "break" SIGINT
while true ;do
log "Start new cycle for checking service"
# 将每行开头的空格去掉
sed 's/^[[:space:]]*//' cfg.csv | while read -r line
do
# 去掉开头是#和空行
if ! [[ $line == "#"* || -z $line ]];then
# 判断该行的参数个数,等于5处理,否则忽略
para_num=$(echo "$line"|sed 's/,/\n/g'|wc -l)
if [[ $para_num == 5 ]];then
# 批量获取多个参数并赋值
IFS=',' read -r run_flag start_cmd curl_flag url judge_content <<< "$line"
# 生成当前时刻的进程列表,并存储为临时文件
ps -ef >".tmp_res"
# 当前如果无符合该运行标志的进程,就运行启动脚本
if ! grep -q "$run_flag" ".tmp_res"; then
log "进程${run_flag}不存在,开始启动"
"${start_cmd}"
else
log "进程${run_flag}存在,继续。。。"
pid=$(grep "$run_flag" ".tmp_res" |grep -v grep|awk '{print $2}'|sed ':a;N;$!ba;s/\n/ /g')
curl_judge "$curl_flag" "$url" "$judge_content" "$pid" "$start_cmd"
fi
log "开始行等待${line_period}s"
sleep "${line_period}s"
fi
fi
done
log "开始文件等待${file_period}s"
sleep "${file_period}s"
done
teste
dfds
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论