Ejemplo n.º 1
0
class ZenStatus(IZen, Client):

	down = []

	hosts = {}

	log = None

	def __init__(self):
		zen = ConfUtil.zen()

		conf = ConfUtil.zen(__file__)

		conf['localhost'] = zen['localhost']

		self.conf = conf

		daemon = Util.filename(__file__)

		self.log = Log(daemon)

		Client.__init__(self, daemon, self.log)

	def mail(self, host, success):
		status = 'up' if success else 'down'

		mail = DictUtil.copy(self.conf['mail'])

		mail['subject'] %= status.upper()
		mail['body'] %= host

		debug = DebugUtil.mail(self.conf['localhost']['name'], mail)

		if Mail.sendraw(mail):
			self.log.info('Mail sent %s' % debug)
		else:
			self.log.critical('Mail not sent %s' % debug)

	def start(self, ip):
		up = False

		host = Util.hostname(ip)

		for _ in range(self.conf['start']['retries']):
			if 'backup' in self.hosts[host] and self.hosts[host]['backup']:
				up = True

				break

			cmd = ['ssh', ip, 'service', 'serviced', 'start']

			check_call(cmd, stderr=PIPE)

			cmd = ['ssh', ip, 'service', 'serviced', 'status']

			output = check_output(cmd, stderr=PIPE)

			if 'running' not in output:
				sleep(self.conf['start']['timeout'])

				continue

			cmd = ['ssh', ip, 'serviced', 'service', 'start', 'Zenoss.core']

			output = check_output(cmd)

			if 'started' not in output:
				sleep(self.conf['start']['timeout'])

				continue

			up = True

			break

		if host in self.down:
			if up:
				self.mail(host, True)

				self.down.remove(host)
		else:
			if not up:
				self.mail(host, False)

				self.down.append(host)

		return up

	def status(self):
		_hosts = self.hosts.keys()

		hosts = {h: {} for h in _hosts}

		for host in sorted(_hosts):
			if not self.hosts[host]['ping']:
				hosts[host]['status'] = False

				self.log.error(host)

				continue

			if self.start(self.hosts[host]['ip']):
				hosts[host]['status'] = True

				self.log.success(host)
			else:
				hosts[host]['status'] = False

				self.log.error(host)

		d = {'hosts': hosts}

		self.send(d)

	def run(self):
		try:
			worker = None

			self.log.clear()

			while True:
				packet = self.recv()

				self.hosts = packet['hosts']

				if worker is not None and worker.is_alive():
					continue

				self.log.clear()

				worker = Thread(target=self.status)

				worker.start()
		except KeyboardInterrupt:
			pass
Ejemplo n.º 2
0
class ZenPing(IZen, Client):

	hosts = {}

	mails = {
		'up'	: [],
		'down'	: []
	}

	down = []

	log = None

	@staticmethod
	def ping(ip):
		try:
			cmd = ['ping', '-c', '1', '-w', '1', '-q', ip]

			output = check_output(cmd)

			return ' 0% packet loss' in output
		except CalledProcessError:
			return False

	def __init__(self):
		zen = ConfUtil.zen()

		conf = ConfUtil.zen(__file__)

		conf['localhost'] = zen['localhost']

		self.conf = conf

		daemon = Util.filename(__file__)

		self.log = Log(daemon)

		Client.__init__(self, daemon, self.log)

	def mail(self, up):
		status = 'up' if up else 'down'

		mail = DictUtil.copy(self.conf['mail'])

		mail['subject'] %= status.upper()

		body = self.mails[status].pop()

		if len(self.mails[status]) > 0:
			hosts = ', '.join(self.mails[status])

			body = '%s & %s' % (hosts, body)

		mail['body'] %= body

		debug = DebugUtil.mail(self.conf['localhost']['name'], mail)

		if Mail.sendraw(mail):
			self.log.info('Mail sent %s' % debug)
		else:
			self.log.critical('Mail not sent %s' % debug)

	def run(self):
		try:
			self.log.clear()

			packet = self.recv()

			self.hosts = packet['hosts']

			hosts = {h: {} for h in self.hosts.keys()}

			while True:
				for host in sorted(self.hosts):
					if ZenPing.ping(self.hosts[host]['ip']):
						if host in self.down:
							self.down.remove(host)

							self.mails['up'].append(host)

						hosts[host]['ping'] = True

						self.log.success(host)
					else:
						if host not in self.down:
							self.down.append(host)

							self.mails['down'].append(host)

						hosts[host]['ping'] = False

						self.log.error(host)

				if len(self.mails['down']) > 0:
					self.mail(False)

					self.mails['down'] = []

				if len(self.mails['up']) > 0:
					self.mail(True)

					self.mails['up'] = []

				d = {'hosts': hosts}

				self.send(d)

				sleep(self.conf['sleep'])

				self.log.clear()
		except KeyboardInterrupt:
			pass