python脚本对域名监控

该博客介绍了一种使用Python编写的脚本,用于监控服务器上的域名和服务的可用性。通过定期执行,脚本可以检查程序是否运行以及域名是否可以正常访问,为运维工作提供了便利。
摘要由CSDN通过智能技术生成

服务器常对外提供tcp和http两种接口,我们判断服务和域名是否可用,有多种方法,比如在服务器上写个程序定期执行ps命令可以判断程序是否在运行, 用访问域名的方式判断域名是否可行。

下面的python程序是我们自己常用的脚本程序, 用来判断域名及服务是否可行,供各位小伙伴一起参考,如有问题,请多多指点,有不清楚也可以留言,我看到会回复

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import thread
import threading
import httplib
import time
import Queue
import json
import os
import sys
import urllib
import socket
import math
import datetime


# 线程列表
threads = []

# 发往钉钉的消息队列
ddQueueLock = threading.Lock()
ddMsgQueue = Queue.Queue(1024)

# 文件日志的消息队列
logQueueLock = threading.Lock()
logMsgQueue = Queue.Queue(1024)


def current_dir():
	return os.path.dirname(os.path.abspath(sys.argv[0]))

#将utc时间转化为北京时间
def beijin_time():
	timenow = (datetime.datetime.utcnow() + datetime.timedelta(hours=8)) 
	return timenow.strftime('%Y-%m-%d %H:%M:%S')

def in_dd_queue(msg):
	bjTime = beijin_time()
	msgFilter = "[北京时间:%s]%s" % (bjTime, msg)
	#ddQueueLock.acquire()
	ddMsgQueue.put(msgFilter)
	#ddQueueLock.release()

def out_dd_queue():
	#ddQueueLock.acquire()
	return ddMsgQueue.get()
	#ddQueueLock.release()

def in_log_queue(msg):
	bjTime = beijin_time()
	msgFilter = "[北京时间:%s]%s" % (bjTime, msg)
	#logQueueLock.acquire()
	logMsgQueue.put(msgFilter)
	#logQueueLock.release()

def out_log_queue():
	#logQueueLock.acquire()
	return logMsgQueue.get()
	#logQueueLock.release()

def httpGetRequest(protocol, host, port, path, reqTimeout, respStatus, respReason, respBody):
	returnCode = returnValue = None
	conn = None
	try:
		if protocol == "https":
			conn = httplib.HTTPSConnection(host, port, timeout=reqTimeout)
		else:
			conn = httplib.HTTPConnection(host, port, timeout=reqTimeout)

		conn.request("GET", path, headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5"})
		result = conn.getresponse()
		status = result.status
		reason = result.reason
		body = result.read().strip()
		
		# 这里的逻辑自定义:实际接收的返回值和期望的返回值的对比
		if (status == respStatus and reason == resp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值