Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / lighttpd / lighttpd_ @ 7063330e

Historique | Voir | Annoter | Télécharger (3,85 ko)

1 87487787 iborodikhin
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# vim: set fileencoding=utf-8
4 5b8adb58 Lars Kruse
"""
5
6
=head1 NAME
7
8
Munin plugin to monitor lighttpd web-server.
9
10
11
=head1 CONFIGURATION
12
13
Configuration parameters:
14
15
    [lighttpd_]
16
    env.status_url - url of lighty's server-status
17
                     (optional, default is http://127.0.0.1/server-status)
18
    env.username - username to provide if status_url requires authentication
19
                   (optional, default - no authentication)
20
    env.password - password to provide if status_url requires authentication
21
                   (optional, default - no authentication)
22
    env.auth_type - the authentication mechanism to use -- either 'basic' (default) or 'digest'.
23
24
Note: If HTTP authentication is required you should specify both username and password.
25
26
27
=head1 INSTALLTION
28
29
Copy file to directory /usr/share/munin/plugins/
30
Because this plugin has "suggest" capability the last step is to run
31
32
    munin-node-configure --suggest --shell | sh -x
33
34
35
=head1 AUTHOR
36
37
Copyright Igor Borodikhin
38
39
40
=head1 LICENSE
41
42
GPLv3
43
44
45
=head1 MAGIC MARKERS
46
  #%# family=contrib
47
  #%# capabilities=autoconf suggest
48
"""
49
50
import os
51
import sys
52
import urllib2
53
54 87487787 iborodikhin
55
program = sys.argv[0]
56 7063330e Lars Kruse
graph_type = program[program.rfind("_") + 1:]
57 87487787 iborodikhin
graph_types = {
58 5b8adb58 Lars Kruse
    "accesses": [
59 87487787 iborodikhin
        {
60 5b8adb58 Lars Kruse
            "title": "Total accesses",
61
            "type": "COUNTER",
62
            "args": "--base 1000 -l 0",
63
            "fields": ["accesses"]
64 87487787 iborodikhin
        }
65
    ],
66 5b8adb58 Lars Kruse
    "kbytes": [
67 87487787 iborodikhin
        {
68 5b8adb58 Lars Kruse
            "title": "Total kBytes",
69
            "type": "COUNTER",
70
            "args": "--base 1024 -l 0",
71
            "fields": ["kbytes"]
72 87487787 iborodikhin
        }
73
    ],
74 5b8adb58 Lars Kruse
    "uptime": [
75 87487787 iborodikhin
        {
76 5b8adb58 Lars Kruse
            "title": "Uptime",
77
            "type": "GAUGE",
78
            "args": "--base 1000 -l 0",
79
            "fields": ["uptime"]
80 87487787 iborodikhin
        }
81
    ],
82 5b8adb58 Lars Kruse
    "status": [
83 87487787 iborodikhin
        {
84 5b8adb58 Lars Kruse
            "title": "Status",
85
            "type": "GAUGE",
86
            "args": "--base 1000 -l 0",
87
            "fields": ["busy", "idle"]
88 87487787 iborodikhin
        }
89
    ]
90
}
91
92 5b8adb58 Lars Kruse
93 87487787 iborodikhin
if len(sys.argv) == 2 and sys.argv[1] == "autoconf":
94 7063330e Lars Kruse
    print("yes")
95 87487787 iborodikhin
elif len(sys.argv) == 2 and sys.argv[1] == "config":
96
    if graph_type not in graph_types.keys():
97 5b8adb58 Lars Kruse
        raise Exception("Unknown graph type '%s'" % graph_type)
98 87487787 iborodikhin
    params = graph_types[graph_type]
99
    for item in params:
100 7063330e Lars Kruse
        print("graph_title %s" % item["title"])
101
        print("graph_category webserver")
102 87487787 iborodikhin
        for field in item["fields"]:
103 7063330e Lars Kruse
            print("%s.label %s" % (field, field))
104
            print("%s.type %s" % (field, item["type"]))
105
        print("graph_args %s" % item["args"])
106 87487787 iborodikhin
elif len(sys.argv) == 2 and sys.argv[1] == "suggest":
107
    for item in graph_types.keys():
108 7063330e Lars Kruse
        print(item)
109 87487787 iborodikhin
else:
110 1f04d10a René 'Necoro' Neumann
    status_url = os.environ.get('status_url', 'http://127.0.0.1/server-status')
111 87487787 iborodikhin
112
    request = urllib2.Request("%s?auto" % status_url)
113
    if "username" in os.environ and "password" in os.environ:
114 1f04d10a René 'Necoro' Neumann
        mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
115
        mgr.add_password(None, status_url, os.environ["username"], os.environ["password"])
116
        if os.environ.get("auth_type", "basic") == "digest":
117
            auth = urllib2.HTTPDigestAuthHandler(mgr)
118
        else:
119
            auth = urllib2.HTTPBasicAuthHandler(mgr)
120
        opener = urllib2.build_opener(auth)
121
        urllib2.install_opener(opener)
122 87487787 iborodikhin
    info = urllib2.urlopen(request).read()
123
    data = {}
124 1f04d10a René 'Necoro' Neumann
    for line in info.split("\n"):
125 87487787 iborodikhin
        try:
126 5b8adb58 Lars Kruse
            (title, value) = line.split(": ")
127
            data[title] = value
128
        except ValueError:
129
            pass
130 87487787 iborodikhin
131
    if graph_type == "accesses":
132 7063330e Lars Kruse
        print("accesses.value %s" % data["Total Accesses"])
133 87487787 iborodikhin
    elif graph_type == "kbytes":
134 7063330e Lars Kruse
        print("kbytes.value %s" % data["Total kBytes"])
135 87487787 iborodikhin
    elif graph_type == "uptime":
136 7063330e Lars Kruse
        print("uptime.value %s" % str(float(data["Uptime"]) / 86400))
137 87487787 iborodikhin
    elif graph_type == "status":
138 7063330e Lars Kruse
        print("busy.value %s" % data["BusyServers"])
139
        print("idle.value %s" % data["IdleServers"])