Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial import

  • Loading branch information...
commit 8a8d914b36cdc2ad06e7273b9d8e1141ddaedbd3 0 parents
@kordless authored
0  README
No changes.
11 app.yaml
@@ -0,0 +1,11 @@
+application: logderwaals
+version: 1
+runtime: python
+api_version: 1
+
+handlers:
+- url: /static/
+ static_dir: static
+
+- url: /.*
+ script: main.py
11 index.yaml
@@ -0,0 +1,11 @@
+indexes:
+
+# AUTOGENERATED
+
+# This index.yaml is automatically updated whenever the dev_appserver
+# detects that a new type of query is run. If you want to manage the
+# index.yaml file manually, remove the above marker line (the line
+# saying "# AUTOGENERATED"). If you want to manage some indexes
+# manually, move them above the marker line. The index.yaml file is
+# automatically uploaded to the admin console when you next deploy
+# your application using appcfg.py.
35 loggly.py
@@ -0,0 +1,35 @@
+import logging, os
+from logging import handlers
+import xml.dom.minidom as minidom
+from google.appengine.api import urlfetch
+
+class LogglyHTTPSHandler(handlers.MemoryHandler):
+ def __init__(self, capacity, flushLevel, target, endpoint):
+ handlers.MemoryHandler.__init__(self, capacity, flushLevel, target)
+ self.appname = os.getcwd().split('/')[-2]
+ self.version = os.getcwd().split('/')[-1]
+ self.endpoint = endpoint
+
+ def flush(self):
+ rpc = urlfetch.create_rpc()
+ stuff = 'source=' + self.appname + '-' + self.version
+ for record in self.buffer:
+ stuff += self.format(record)
+ urlfetch.make_fetch_call(rpc, url=self.endpoint, payload=stuff, method=urlfetch.POST)
+ self.buffer = []
+
+class LogglyLogger():
+ def __init__(self, endpoint, level):
+ self.endpoint = endpoint
+ self.level = level
+ logger = logging.getLogger()
+ lh = LogglyHTTPSHandler(100, level, None, endpoint)
+ formatterStr = '''%(asctime)s level=%(levelname)s, msg="%(message)s", module=%(module)s, file="%(filename)s", lineno=%(lineno)d'''
+ formatter = logging.Formatter(formatterStr)
+ lh.setFormatter(formatter)
+ logger.addHandler(lh)
+ logger.setLevel(level)
+ self.lh = lh
+
+ def flush(self):
+ self.lh.flush()
127 main.py
@@ -0,0 +1,127 @@
+#!/usr/bin/env python
+#
+# Copyright 2010 Loggly Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+import cgi, os, datetime, time
+import logging, loggly
+import urllib2, base64, urllib
+from django.utils import simplejson
+from google.appengine.api import urlfetch
+from google.appengine.api import users
+from google.appengine.ext import webapp
+from google.appengine.ext.webapp.util import run_wsgi_app
+from google.appengine.ext import db
+from google.appengine.ext.webapp import template
+
+# variable things
+appengineurl = 'logderwaals.appspot.com'
+logglyaccount = 'geekceo.loggly.com'
+
+# grab data for a geckoboard graph object
+class GraphHandler(webapp.RequestHandler):
+ def get(self):
+ q = self.request.get('q')
+ if q == "": q = "*"
+
+ # limit the query to just the web server's input
+ q = q + " AND inputname:loggly_web"
+ logging.info("executing a query to loggly for: %s" % q)
+ query = urllib.quote(q)
+
+ # auth stuff
+ username = 'kordless'
+ password = 'password'
+ base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
+
+ # query last day and convert to json
+ request = urllib2.Request("http://%s/api/facets/date?q=%s&from=NOW-1DAY&until=NOW" % (logglyaccount, query))
+ request.add_header("Authorization", "Basic %s" % base64string)
+ result = urllib2.urlopen(request)
+ json_result = simplejson.loads(result.read())
+
+ # sort dict, then shovel it over to the template
+ stuff = []
+ for item in sorted(json_result['data'].items()):
+ stuff.append(item)
+ from_date = stuff[0][0] #first date
+ until_date = stuff[-1][0] #last date
+ template_values = {'stuff': stuff, 'from_date': from_date, 'until_date': until_date}
+ path = os.path.join(os.path.dirname(__file__), 'templates/graph.xml')
+ self.response.headers['Content-Type'] = 'text/xml'
+ self.response.out.write(template.render(path, template_values))
+ def post(self):
+ self.get()
+
+# grab data for a geckoboard meter object
+class MeterHandler(webapp.RequestHandler):
+ def get(self):
+ q = self.request.get('q')
+ if q == "": q = "*"
+
+ # limit the query to just the web server's input
+ q = q + " AND inputname:loggly_web"
+ logging.info("executing a query to loggly for: %s" % q)
+ query = urllib.quote(q)
+
+ # auth stuff
+ username = 'kordless'
+ password = 'password'
+ base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
+
+ # query last hour
+ request = urllib2.Request("http://%s/api/facets/date?q=%s&from=NOW-1HOUR&until=NOW" % (logglyaccount, query))
+ request.add_header("Authorization", "Basic %s" % base64string)
+ result = urllib2.urlopen(request)
+ stuff = simplejson.loads(result.read())
+ numfound = stuff['numFound']
+
+ # query last day
+ request = urllib2.Request("http://%s/api/facets/date?q=%s&from=NOW-1DAY&until=NOW" % (logglyaccount, query))
+ request.add_header("Authorization", "Basic %s" % base64string)
+ result = urllib2.urlopen(request)
+ stuff = simplejson.loads(result.read())
+ numfoundlastday = stuff['numFound']
+
+ # shovel it over to the template
+ template_values = {'numfound': numfound, 'numfoundlastday': numfoundlastday}
+ path = os.path.join(os.path.dirname(__file__), 'templates/meter.xml')
+ self.response.headers['Content-Type'] = 'text/xml'
+ self.response.out.write(template.render(path, template_values))
+ def post(self):
+ self.get()
+
+
+class MainPage(webapp.RequestHandler):
+ def get(self):
+ logging.info("a noob is knocking")
+ self.response.out.write("This is a Loggly Gecko mashup example. Try viewing some raw XML data: <br/><br/><a href='/meter?q=404'>a meter for '404' error codes</a><br/><br/> <a href='/graph?q=wiki AND POST'>a graph for wiki edits</a>")
+ def post(self):
+ self.get()
+
+
+application = webapp.WSGIApplication( [('/', MainPage), ('/meter', MeterHandler), ('/graph', GraphHandler)], debug=True)
+
+def main():
+ # set up logging to Loggly and log the time on the appengine box
+ hoover = loggly.LogglyLogger('http://logs.loggly.com/inputs/d83ef1fb-5214-4106-9d32-6956201c29ce', logging.INFO)
+ rightnow = time.time()
+ logging.info('time=%s' % (rightnow) )
+
+ run_wsgi_app(application)
+
+
+if __name__ == "__main__":
+ main()
+
11 templates/graph.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<root>
+ {% for x in stuff %}
+ <item>{{x.1}}</item>
+ {% endfor %}
+ <settings>
+ <axisx>{{from_date}}</axisx>
+ <axisx>{{until_date}}</axisx>
+ <colour>990000</colour>
+ </settings>
+</root>
8 templates/meter.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<root>
+ <item>{{numfound}}</item>
+ <max>
+ <value>{{numfoundlastday}}</value>
+ <text>{{query}} Last Day</text>
+ </max>
+</root>
Please sign in to comment.
Something went wrong with that request. Please try again.