Projet

Général

Profil

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

root / plugins / syncthing / syncthing_ @ e527db57

Historique | Voir | Annoter | Télécharger (4,04 ko)

1
#!/bin/sh
2
# -*- sh -*-
3
: <<=cut
4
=head1 NAME
5
syncthing_ - Plugin to monitor Syncthing server
6

    
7
=head1 DESCRIPTION
8
This plugin gathers metrics from a Syncthing server.
9

    
10
This plugin requires the jq utility : https://stedolan.github.io/jq/
11
This plugin requires the cURL utility : https://curl.haxx.se/
12

    
13
Available plugins :
14
syncthing_cpu       # 
15
syncthing_mem       #
16
syncthing_goroutine #
17
syncthing_transfer  #
18
syncthing_uptime    #
19

    
20
=head1 CONFIGURATION
21
To make the plugin connect to the Syncthing server one has to use this type of 
22
configuration
23
[syncthing_*]
24

    
25
env.syncthing_apikey myapikey0123456789
26
env.syncthing_host 127.0.0.1
27
env.syncthing_port 8384
28
env.syncthing_proto http
29

    
30
=head1 AUTHOR
31
Pierre-Alain TORET <pierre-alain.toret@protonmail.com>
32

    
33
=head1 LICENSE
34
MIT
35
=cut
36

    
37
getstatus() {
38
    "$CURL" -s -X GET -H "X-API-Key: $syncthing_apikey" "$syncthing_proto://$syncthing_host:$syncthing_port/rest/system/status"
39
}
40

    
41
cpu() {
42
    case $1 in
43
        config)
44
            cat <<'EOM'
45
graph_title Syncthing server cpu usage
46
graph_args -u 100
47
graph_vlabel %
48
graph_category network
49
syncthing_cpu.label cpu
50
EOM
51
            exit 0;;
52
        *)
53
            STATUS=$(getstatus)
54
			CPU=$(echo "$STATUS" | "$JQ" '.cpuPercent')
55
            printf "syncthing_cpu.value %s\n" "$CPU"
56
    esac
57
}
58

    
59
mem() {
60
    case $1 in
61
        config)
62
            cat <<'EOM'
63
graph_title Syncthing server memory
64
graph_category network
65
graph_order syncthing_mem_all syncthing_mem_sys
66
graph_args --base 1000
67
graph_vlabel bits
68
syncthing_mem_all.label allocated
69
syncthing_mem_all.cdef syncthing_mem_all,8,*
70
syncthing_mem_sys.label obtained
71
syncthing_mem_sys.cdef syncthing_mem_sys,8,*
72
EOM
73
            exit 0;;
74
	    *)
75
            STATUS=$(getstatus)
76
            ALL=$(echo "$STATUS" | "$JQ" '.alloc')
77
            SYS=$(echo "$STATUS" | "$JQ" '.sys')
78
            printf "syncthing_mem_all.value %s\n" "$ALL"
79
            printf "syncthing_mem_sys.value %s\n" "$SYS"
80
    esac
81
}
82

    
83
uptime() {
84
    case $1 in
85
        config)
86
            cat <<'EOM'
87
graph_title Syncthing server uptime
88
graph_vlabel uptime in seconds
89
graph_category network
90
syncthing_uptime.label uptime
91
EOM
92
            exit 0;;
93
        *)
94
            STATUS=$(getstatus)
95
            UPTIME=$(echo "$STATUS" | "$JQ" '.uptime')
96
            printf "syncthing_uptime.value %s\n" "$UPTIME"
97
    esac
98
}
99

    
100
goroutine() {
101
    case $1 in
102
        config)
103
            cat <<'EOM'
104
graph_title Syncthing server go routines
105
graph_vlabel number of go routines
106
graph_category network
107
syncthing_goroutine.label routines 
108
EOM
109
            exit 0;;
110
        *)
111
            STATUS=$(getstatus)
112
            GOROUTINES=$(echo "$STATUS" | "$JQ" '.goroutines')
113
            printf "syncthing_goroutine.value %s\n" "$GOROUTINES"
114
    esac
115
}
116

    
117
transfer() {
118
    case $1 in
119
        config)
120
            cat <<'EOM'
121
graph_title Syncthing server total transfer
122
graph_category network
123
graph_order syncthing_transfer_down syncthing_transfer_up
124
graph_args --base 1000
125
graph_vlabel bits in (-) / out (+) per ${graph_period}
126
syncthing_transfer_down.label received
127
syncthing_transfer_down.type COUNTER
128
syncthing_transfer_down.graph no
129
syncthing_transfer_down.cdef syncthing_transfer_down,8,*
130
syncthing_transfer_up.label bps
131
syncthing_transfer_up.type COUNTER
132
syncthing_transfer_up.negative syncthing_transfer_down
133
syncthing_transfer_up.cdef syncthing_transfer_up,8,*
134
EOM
135
            exit 0;;
136
	    *)
137
            CONNECTIONS=$("$CURL" -s -X GET -H "X-API-Key: $syncthing_apikey" "$syncthing_proto://$syncthing_host:$syncthing_port/rest/system/connections")
138
            IBT=$(echo "$CONNECTIONS" | "$JQ" '.total | .inBytesTotal')
139
            OBT=$(echo "$CONNECTIONS" | "$JQ" '.total | .outBytesTotal')
140
            printf "syncthing_transfer_up.value %s\\n" "$IBT"
141
            printf "syncthing_transfer_down.value %s\\n" "$OBT"
142
    esac
143
}
144

    
145
cd "$(dirname "$0")" || exit
146

    
147
CURL=$(which curl)
148
JQ=$(which jq)
149

    
150
case $(basename "$0") in 
151
    syncthing_cpu)
152
	cpu "$1"
153
	exit 0;;
154
    syncthing_mem)
155
	mem "$1"
156
	exit 0;;
157
    syncthing_uptime)
158
	uptime "$1"
159
	exit 0;;
160
    syncthing_goroutine)
161
	goroutine "$1"
162
	exit 0;;
163
    syncthing_transfer)
164
	transfer "$1"
165
	exit 0;;
166
esac