From f955b197df147214c822d5758f7da97da3e6d0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guido=20G=C3=BCnther?= Date: Sat, 24 Oct 2015 20:55:36 +0200 Subject: Initial commit --- README.md | 7 +++++ cur_rate_ | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 README.md create mode 100755 cur_rate_ diff --git a/README.md b/README.md new file mode 100644 index 0000000..653012a --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +Munin plugin to plot currency exchange rates + +This plots currency exchange rates using the Yahoo APIs. + +If you e.g. want the conversion rate between Euro and CHF just link it like: + + ln -s /usr/share/munin/plugins/cur_rate_ /etc/munin/plugins/cur_rate_eurchf diff --git a/cur_rate_ b/cur_rate_ new file mode 100755 index 0000000..4fb9a09 --- /dev/null +++ b/cur_rate_ @@ -0,0 +1,102 @@ +#!/usr/bin/python +# vim: set fileencoding=utf-8 : +# +# Munin plugin to monitor currency exchange rates using the +# yahoo finance api +# +# Copyright 2015 Guido Günther +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# 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, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# depends: python-requests +# +#%# capabilities=autoconf +#%# family=contrib + +import requests +import os +import re +import sys +from xml.etree import ElementTree + +url_pattern = 'https://query.yahooapis.com/v1/public/yql?q=select%%20*%%20from%%20yahoo.finance.xchange%%20where%%20pair%%20in%%20%%28%%22%s%%22%%29&env=store://datatables.org/alltableswithkeys' + +def print_config(url): + out = fetch_raw(url) + xml = ElementTree.XML(out) + namexml = xml.find('./results/rate/Name') + f, t = namexml.text.split('/') + + print """graph_title Currency exchange rate %s to %s +graph_vlabel %s +graph_category Finance +graph_info This graph shows the currency exchange rate between %s and %s""" % (f,t,t,f,t) + + print "%s%s_rate.label %s/%s (rate)" % (f, t, f, t) + print "%s%s_rate.type GAUGE" % (f, t) + print "%s%s_rate.draw LINE1" % (f, t) + print "%s%s_ask.label %s/%s (ask)" % (f, t, f, t) + print "%s%s_ask.type GAUGE" % (f, t) + print "%s%s_ask.draw LINE1" % (f, t) + print "%s%s_bid.label %s/%s (bid)" % (f, t, f, t) + print "%s%s_bid.type GAUGE" % (f, t) + print "%s%s_bid.draw LINE1" % (f, t) + +def fetch_raw(url): + # The items to fetch are passed as simple json string + r = requests.get(url) + return r.text + +def fetch_values(url): + out = fetch_raw(url) + xml = ElementTree.XML(out) + namexml = xml.find('./results/rate/Name') + f, t = namexml.text.split('/') + rate = xml.find('./results/rate/Rate').text + ask = xml.find('./results/rate/Ask').text + bid = xml.find('./results/rate/Bid').text + print "%s%s_rate.value %s" % (f, t, rate) + print "%s%s_ask.value %s" % (f, t, ask) + print "%s%s_bid.value %s" % (f, t, bid) + + +def main(args): + exchange = sys.argv[0].rsplit('_')[-1].upper() + url = url_pattern % exchange + if len(args) > 1: + if args[1] in [ 'autoconf', 'detect' ]: + try: + fetch_raw(url) + print "yes" + return 0 + except: + print "no" + return 1 + elif args[1] == 'config': + try: + print_config(url) + except Exception as e: + print >>sys.stderr, "Failed to fetch config: '%s'" % e + return 1 + return 0 + try: + fetch_values(url) + except Exception as e: + print >>sys.stderr, "Failed to fetch values: '%s'" % e + return 1 + return 0 + +if __name__ == "__main__": + sys.exit(main(sys.argv)) -- cgit v1.2.1