Commits

Denis Khabarov  committed 8cfd49d

modified: etherway.py
Небольшие исправления
new file: munin-etherway
Плагин для munin

  • Participants
  • Parent commits 5636c6a

Comments (0)

Files changed (2)

 __copyright__ = "Copyright © 2012 Denis 'Saymon21' Khabarov"
 __credits__ = []
 __license__ = "GPLv3"
-__version__ = "0.1"
+__version__ = "0.2"
 __maintainer__ = "Denis 'Saymon21' Khabarov"
 __email__ = "saymon@hub21.ru"
 __status__ = "Development"
 		return repr(self.value)
 
 class EtherwayBalanceFetcher:
-	def __init__(self, login, password):
+	def __init__(self, login, password, timeout = 10):
+		if login is None:
+			raise QueryError('Login is None, but expected string')
+		if password is None:
+			raise QueryError('Password is None, but expected string')
 		self.login = login
 		self.password = password
-		if self.login is None:
-			raise QueryError("Please enter login!")
-		if self.password is None:
-			raise QueryError("Please enter password!")
-		
+		self.timeout = timeout
+
 	def auth(self):
 		urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor))
 		params = urllib.urlencode({'LoginForm[username]': self.login, 'LoginForm[password]': self.password})
 		request = urllib2.Request('https://lk.etherway.ru/site/login', params)
 		try:
-			result = urllib2.urlopen(request)
+			result = urllib2.urlopen(request, timeout = self.timeout)
 		except (urllib2.URLError,urllib2.HTTPError) as errstr:
 			raise QueryError(errstr)
-		text = result.read()
-		return text
+		return result.read()
 
 	def get_balance(self):
 		source = self.auth()
 		if source:
-			sbalobj=re.search("<div id='balance'>.+<span class=green>(.+)</span>",source)
-			balance=sbalobj.group(1).split(" ")
-			if balance[0] is not None: 
-				return balance[0].encode('ascii','ignore')
+			sbalobj=re.search("<div id='balance'>.+<span class=\S+>(.+)</span>", source)
+			if sbalobj is not None:
+				balance=sbalobj.group(1).split(" ")
+				if balance[0] is not None:				
+					return balance
+			else:
+				raise QueryError('Not found')
+				
+				
+# Example usage:
+# ./etherway.py mylogin mypassword				
+if __name__ == "__main__":
+	import sys
+	if not len(sys.argv) < 3:
+		fetcher = EtherwayBalanceFetcher(login = sys.argv[1],password = sys.argv[2])
+		try:
+			data = fetcher.get_balance()
+		except QueryError as err_msg:
+			print('Error: ' + str(err_msg))
+			sys.exit(1)	
+		print('Your balance: ' + str(data[0]))
+		sys.exit(0)
+	else:
+		print('Usage ' + sys.argv[0] + ' login password')
+		sys.exit(1)

File munin-etherway

+#!/usr/bin/env python
+#-*- coding: utf8 -*-
+# MuninEtherway - Simple plugin for Munin Monitoring
+
+# Copyright © 2012 Denis 'Saymon21' Khabarov
+# E-Mail: saymon at hub21 dot ru (saymon@hub21.ru)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3
+# as published by the Free Software Foundation.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+'''
+Config for /etc/munin/plugin-conf.d/munin-node:
+
+[etherway]
+	env.ETHERWAY_USER pUSERID
+	env.ETHERWAY_PASSW mysuperpassword
+'''
+
+import etherway
+from os import getenv
+from sys import argv
+
+def main():
+	if len(argv) == 2 and argv[1] == 'config':
+		print('graph_title Etherway Balance')
+		print('graph_args --base 1024 -r')
+		print('graph_category network')
+		print('graph_info This graph shows the number of balance on etherway.ru for user %s.' %(getenv('ETHERWAY_USER')))
+		print('RUB.label RUB')
+		print('RUB.type GAUGE')
+		print('RUB.info RUB')
+		exit()
+	else:
+		value = 0
+		lk=etherway.EtherwayBalanceFetcher(login=getenv('ETHERWAY_USER'), password=getenv('ETHERWAY_PASSW'))
+		try:
+			mybalance = lk.get_balance()
+		except etherway.QueryError:
+			print('RUB.value 0')
+			exit()
+		value = mybalance[0].split(".")
+		value = int(value[0])
+		print('RUB.value %d' % value)
+	
+if __name__ == "__main__":
+	main()