# Python 파일 (mqtt_health_chk.py)
import paho.mqtt.client as mqtt
import sys
url = sys.argv[1]
port = int(sys.argv[2])
username = sys.argv[3]
password = sys.argv[4]
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("ok")
else:
print("fail code=", rc)
try:
client = mqtt.Client()
client.on_connect = on_connect
client.username_pw_set(username, password)
client.connect(host=url, port=port)
client.loop_start()
client.publish('health_chk', 'ok', 1)
client.loop_stop()
client.disconnect()
except:
print("fail")
exit(1)
# paho-mqtt 설치
$ pip3 install paho-mqtt==1.6.1
# jenkins job 등록
#!/bin/bash
url='10.0.0.1'
port=1883
username='admin'
password='pass'
attempts=5
timeout=10
online=false
echo "Checking status of $url."
for (( i=1; i<=$attempts; i++ ))
do
code=`python3 /home/ubuntu/shell/mqtt_health_chk.py $url $port $username $password`
if [ "$code" = "ok" ]; then
online=true
break
else
sleep $timeout
fi
done
if $online; then
echo "Monitor finished, MQTT is online."
exit 0 # Build Success
else
echo "Monitor failed, MQTT seems to be down."
exit 1 # Build Failed
fi
# jenkins 빌드 유발
H/10 * * * *
# 빌드 후 조치 등록 (Slack)
'Infra Structure > Jenkins' 카테고리의 다른 글
[Jenkins] build timeout 플러그인 (0) | 2022.07.21 |
---|---|
타 계정으로 리눅스 명령어 실행 (0) | 2022.07.21 |
[Jenkins] Tomcat restart (0) | 2022.07.09 |
[Jenkins] Server Disk 용량 체크 (0) | 2022.07.09 |
[Jenkins] Pipeline 예제(tomcat) try catch (0) | 2022.07.09 |