File: graphite_uwsgi.py

package info (click to toggle)
uwsgi 2.0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,684 kB
  • sloc: ansic: 87,027; python: 7,001; cpp: 1,131; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (34 lines) | stat: -rw-r--r-- 1,004 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# uwsgi --master --chdir /opt/graphite/webapp/graphite --module graphite_uwsgi ...
#
#
# this module will update a carbon server (used by the graphite tool: http://graphite.wikidot.com/)
# with requests count made by a Django app (and can track graphite itself as it is a Django app ;)
#
#

import os
import uwsgi
import time
from django.core.handlers.wsgi import WSGIHandler

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

CARBON_SERVER = "127.0.0.1:2003"


def update_carbon(signum):
    # connect to the carbon server
    carbon_fd = uwsgi.connect(CARBON_SERVER)
    # send data to the carbon server
    uwsgi.send(carbon_fd, "uwsgi.%s.requests %d %d\n" % (uwsgi.hostname, uwsgi.total_requests(), int(time.time())))
    # close the connection with the carbon server
    uwsgi.close(carbon_fd)

# register a new uwsgi signal (signum: 17)
uwsgi.register_signal(17, '', update_carbon)

# attach a timer of 10 seconds to signal 17
uwsgi.add_timer(17, 10)

# the Django app
application = WSGIHandler()