Example #1
0
	def __init__(self):
		self.conf = ConfUtil.zen(__file__)

		daemon = Util.filename(__file__)

		self.log = Log(daemon)

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

		self.server = Server(
			self.conf['snpp']['host'],
			self.conf['snpp']['port']
		)
Example #2
0
class ZenSNPP(IZen, Client):

	server = None

	log = None

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

		daemon = Util.filename(__file__)

		self.log = Log(daemon)

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

		self.server = Server(
			self.conf['snpp']['host'],
			self.conf['snpp']['port']
		)

	def get(self):
		boxes = []

		for box in sorted(self.conf['boxes']):
			url = 'http://%s:81/gsm1.html' % self.conf['boxes'][box]

			try:
				response = urlopen(url, timeout=self.conf['timeout'])

				if response.getcode() != 200:
					continue
			except URLError:
				continue

			html = response.read()

			if 'ERROR' in html:
				continue

			info = html.split()

			_, _, _, _, _, _, _, signal, _ = info

			signal = signal.replace(',', '.')
			signal = float(signal)
			signal = int(signal)

			if signal < 2 or signal == 99:
				continue

			boxes.append(box)

		if len(boxes) == 0:
			raise MyException

		return boxes

	def post(self, box, to, data):
		to = ';'.join(map(str, to))

		url = 'http://%s/source/send_sms.php' % self.conf['boxes'][box]

		data = {
			'username'	: 'admin',
			'pwd'		: 'zenoss',
			'from'		: 'Zenoss',
			'nphone'	: to,
			'outCh'		: 'GSM1',
			'testo'		: data
		}

		data = urlencode(data)

		request = Request(url, data)

		try:
			response = urlopen(request, timeout=self.conf['timeout'])

			if response.getcode() != 200:
				raise MyException
		except URLError:
			raise MyException

	def sms(self, host, allow, packet):
		i = isinstance(packet['to'], int)

		targets = [packet['to']] if i else packet['to']['phones']

		if not allow:
			debug = DebugUtil.sms(host, targets, packet['data'])

			self.log.info('Message dropped %s' % debug)

			return

		try:
			boxes = self.get()

			if all(d not in packet['data'] for d in self.conf['devices']):
				boxes = [boxes.pop(0)]

			for box in boxes:
				self.post(box, targets, packet['data'])

				smsfrom = {
					'host'	: host,
					'box'	: box
				}

				debug = DebugUtil.sms(smsfrom, targets, packet['data'])

				self.log.info('Message sent %s' % debug)
		except MyException:
			if i:
				self.log.critical('Test message not sent')

				return

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

			mail['to'] = packet['to']['mails']
			mail['body'] = packet['data']

			debug = DebugUtil.mail(host, 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()

			while True:
				read = self.server.select()

				for socket in read:
					if self.server.accept(socket):
						try:
							client, ip, packet = self.server.recv()

							client = ClientProxy(client)

							self.server.attach(client)

							host = Util.hostname(ip)

							d = {'host': host}

							self.send(d)

							pkt = self.recv()

							self.sms(host, pkt['allow'], packet)
						except MyException:
							pass
					else:
						client = ClientProxy(socket)

						self.server.dettach(client)
		except KeyboardInterrupt:
			pass