Projet

Général

Profil

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

root / plugins / lighttpd / lighttpd_ @ c6f88968

Historique | Voir | Annoter | Télécharger (3,92 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 8713eb37 Lars Kruse
=head1 INSTALLATION
28 5b8adb58 Lars Kruse
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 c6f88968 Lars Kruse
GNU General Public License v3.0 only
43
44
SPDX-License-Identifier: GPL-3.0-only
45 5b8adb58 Lars Kruse
46
47
=head1 MAGIC MARKERS
48 c6f88968 Lars Kruse
49 5b8adb58 Lars Kruse
  #%# family=contrib
50
  #%# capabilities=autoconf suggest
51 c6f88968 Lars Kruse
52
=cut
53 5b8adb58 Lars Kruse
"""
54
55
import os
56
import sys
57
import urllib2
58
59 87487787 iborodikhin
60
program = sys.argv[0]
61 7063330e Lars Kruse
graph_type = program[program.rfind("_") + 1:]
62 87487787 iborodikhin
graph_types = {
63 5b8adb58 Lars Kruse
    "accesses": [
64 87487787 iborodikhin
        {
65 5b8adb58 Lars Kruse
            "title": "Total accesses",
66
            "type": "COUNTER",
67
            "args": "--base 1000 -l 0",
68
            "fields": ["accesses"]
69 87487787 iborodikhin
        }
70
    ],
71 5b8adb58 Lars Kruse
    "kbytes": [
72 87487787 iborodikhin
        {
73 5b8adb58 Lars Kruse
            "title": "Total kBytes",
74
            "type": "COUNTER",
75
            "args": "--base 1024 -l 0",
76
            "fields": ["kbytes"]
77 87487787 iborodikhin
        }
78
    ],
79 5b8adb58 Lars Kruse
    "uptime": [
80 87487787 iborodikhin
        {
81 5b8adb58 Lars Kruse
            "title": "Uptime",
82
            "type": "GAUGE",
83
            "args": "--base 1000 -l 0",
84
            "fields": ["uptime"]
85 87487787 iborodikhin
        }
86
    ],
87 5b8adb58 Lars Kruse
    "status": [
88 87487787 iborodikhin
        {
89 5b8adb58 Lars Kruse
            "title": "Status",
90
            "type": "GAUGE",
91
            "args": "--base 1000 -l 0",
92
            "fields": ["busy", "idle"]
93 87487787 iborodikhin
        }
94
    ]
95
}
96
97 5b8adb58 Lars Kruse
98 87487787 iborodikhin
if len(sys.argv) == 2 and sys.argv[1] == "autoconf":
99 7063330e Lars Kruse
    print("yes")
100 87487787 iborodikhin
elif len(sys.argv) == 2 and sys.argv[1] == "config":
101
    if graph_type not in graph_types.keys():
102 5b8adb58 Lars Kruse
        raise Exception("Unknown graph type '%s'" % graph_type)
103 87487787 iborodikhin
    params = graph_types[graph_type]
104
    for item in params:
105 7063330e Lars Kruse
        print("graph_title %s" % item["title"])
106
        print("graph_category webserver")
107 87487787 iborodikhin
        for field in item["fields"]:
108 7063330e Lars Kruse
            print("%s.label %s" % (field, field))
109
            print("%s.type %s" % (field, item["type"]))
110
        print("graph_args %s" % item["args"])
111 87487787 iborodikhin
elif len(sys.argv) == 2 and sys.argv[1] == "suggest":
112
    for item in graph_types.keys():
113 7063330e Lars Kruse
        print(item)
114 87487787 iborodikhin
else:
115 1f04d10a René 'Necoro' Neumann
    status_url = os.environ.get('status_url', 'http://127.0.0.1/server-status')
116 87487787 iborodikhin
117
    request = urllib2.Request("%s?auto" % status_url)
118
    if "username" in os.environ and "password" in os.environ:
119 1f04d10a René 'Necoro' Neumann
        mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
120
        mgr.add_password(None, status_url, os.environ["username"], os.environ["password"])
121
        if os.environ.get("auth_type", "basic") == "digest":
122
            auth = urllib2.HTTPDigestAuthHandler(mgr)
123
        else:
124
            auth = urllib2.HTTPBasicAuthHandler(mgr)
125
        opener = urllib2.build_opener(auth)
126
        urllib2.install_opener(opener)
127 87487787 iborodikhin
    info = urllib2.urlopen(request).read()
128
    data = {}
129 1f04d10a René 'Necoro' Neumann
    for line in info.split("\n"):
130 87487787 iborodikhin
        try:
131 5b8adb58 Lars Kruse
            (title, value) = line.split(": ")
132
            data[title] = value
133
        except ValueError:
134
            pass
135 87487787 iborodikhin
136
    if graph_type == "accesses":
137 7063330e Lars Kruse
        print("accesses.value %s" % data["Total Accesses"])
138 87487787 iborodikhin
    elif graph_type == "kbytes":
139 7063330e Lars Kruse
        print("kbytes.value %s" % data["Total kBytes"])
140 87487787 iborodikhin
    elif graph_type == "uptime":
141 7063330e Lars Kruse
        print("uptime.value %s" % str(float(data["Uptime"]) / 86400))
142 87487787 iborodikhin
    elif graph_type == "status":
143 7063330e Lars Kruse
        print("busy.value %s" % data["BusyServers"])
144
        print("idle.value %s" % data["IdleServers"])