Projet

Général

Profil

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

root / plugins / nextcloud / nextcloud_ @ a7d44887

Historique | Voir | Annoter | Télécharger (7,35 ko)

1
#!/bin/sh
2

    
3
set -e
4

    
5
: << =cut
6

    
7
=head1 NAME
8

    
9
nextcloud_ - Monitor usage of nextcloud instances
10

    
11
=head1 APPLICABLE SYSTEMS
12

    
13
Nexcloud instances
14

    
15
=head1 CONFIGURATION
16

    
17
Requires installed curl and jq, a command-line json processor.
18

    
19
This is a wildcard plugin. To monitor a nextcloud instance, link
20
nextcloud_<nextcloud-domain> to this file. You can even append a port 
21
(:8443) to the file if needed. For example,
22

    
23
  ln -s /usr/share/munin/plugins/nextcloud_ \
24
        /etc/munin/plugins/nextcloud_cloud.domain.tld
25

    
26
Set username and password in your munin-node configuration
27

    
28
[nextcloud_cloud.domain.tld]
29
env.username <nexcloud_user>
30
env.password <nextcloud_password>
31
env.api_path <default: /ocs/v2.php/apps/serverinfo/api/v1/info>
32
env.timeout <default: 2s>
33

    
34
It's advised to set an app password (for this plugin) in your nextcloud 
35
instance and not to use the "real" password of your nextcloud user.
36

    
37
=head1 AUTHOR
38

    
39
Copyright (C) 2020 Sebastian L. (https://momou.ch)
40

    
41
=head1 LICENSE
42

    
43
GPLv2
44

    
45
=head1 MAGIC MARKERS
46

    
47
 #%# family=manual
48
 #%# capabilities=autoconf
49

    
50
=cut
51

    
52
# shellcheck disable=SC1090
53
. "$MUNIN_LIBDIR/plugins/plugin.sh"
54

    
55
if [ "${MUNIN_DEBUG:-0}" = 1 ]; then
56
    set -x
57
fi
58

    
59
API_PATH="${api_path:-/ocs/v2.php/apps/serverinfo/api/v1/info}?format=json"
60
DOMAIN="${0##*nextcloud_}"
61
TIMEOUT="${timeout:-2}"
62
CLEANDOMAIN="$(clean_fieldname "$DOMAIN")"
63
USERNAME="${username:-}"
64
PASSWORD="${password:-}"
65

    
66
print_json_data() {
67
    # shellcheck disable=SC2039
68
    local FIRST="$1"
69
    [ -z "$FIRST" ] && exit 0
70
    shift 1
71
    for KEY in "$@"; do
72
        VALUE=$(echo "$FIRST" | jq -cr ".$KEY")
73
        echo "$KEY.value $VALUE"
74
   done
75
}
76

    
77
fetch_url () {
78
    curl -s -f -m "${TIMEOUT}" "$@"
79
}
80

    
81
case $1 in
82

    
83
    autoconf)
84
	if [ -x "$(command -v curl)" ]; then
85
	    if [ -x "$(command -v jq)" ]; then
86
		    (fetch_url -I -u "$USERNAME:$PASSWORD" -I "https://${DOMAIN}${API_PATH}" \
87
			    | grep -iq "Content-Type: application/json" \
88
			    || fetch_url -I -u "$USERNAME:$PASSWORD" -I "http://${DOMAIN}${API_PATH}" \
89
			    | grep -iq "Content-Type: application/json") \
90
			    && echo "yes" && exit 0 \
91
			|| echo "no (invalid or empty response from nextlcoud serverinfo api)" && exit 0
92
            else
93
                echo "no (jq not found)" && exit 0
94
            fi
95
        else
96
            echo "no (curl not found)" && exit 0
97
        fi
98
        ;;
99

    
100
   config)
101

    
102
cat << EOM
103
multigraph nextcloud_users_$CLEANDOMAIN
104
graph_title Nextcloud users on $DOMAIN
105
graph_args --base 1000 -l 0
106
graph_printf %.0lf
107
graph_vlabel connected users
108
graph_info number of connected user
109
graph_category cloud
110
last5minutes.label last 5 minutes
111
last5minutes.info users connected in the last 5 minutes
112
last5minutes.min 0
113
last1hour.label last hour
114
last1hour.info users connected in the last hour
115
last1hour.min 0
116
last24hours.label last 24 hours
117
last24hours.info users connected in the last 24 hours
118
last24hours.min 0
119
num_users.label number of users
120
num_users.info total number of users
121
num_users.min 0
122
multigraph nextcloud_files_$CLEANDOMAIN
123
graph_title Nextcloud files on $DOMAIN
124
graph_args --base 1000 -l 0
125
graph_printf %.0lf
126
graph_vlabel number of files
127
graph_info number of files
128
graph_category cloud
129
num_files.label number of files
130
num_files.info current number of files
131
num_files.min 0
132
multigraph nextcloud_shares_$CLEANDOMAIN
133
graph_title Nextcloud shares on $DOMAIN
134
graph_args --base 1000 -l 0
135
graph_printf %.0lf
136
graph_vlabel number of shares
137
graph_info number of shares
138
graph_category cloud
139
num_shares.label total number of shares
140
num_shares.info current over all total of shares
141
num_shares.min 0
142
num_shares_user.label user shares
143
num_shares_user.info current total of user shares
144
num_shares_user.min 0
145
num_shares_groups.label group shares
146
num_shares_groups.info current total of group shares
147
num_shares_groups.min 0
148
num_shares_link.label link shares
149
num_shares_link.info current total of link shares
150
num_shares_link.min 0
151
num_shares_mail.label mail shares
152
num_shares_mail.info current total of mail shares
153
num_shares_mail.min 0
154
num_shares_room.label room shares
155
num_shares_room.info current total of room shares
156
num_shares_room.min 0
157
num_shares_link_no_password.label link shares without password protection
158
num_shares_link_no_password.info current total of link shares without password protection
159
num_shares_link_no_password.min 0
160
num_fed_shares_sent.label federated shares sent
161
num_fed_shares_sent.info current total of federated shares sent
162
num_fed_shares_sent.min 0
163
num_fed_shares_received.label federated shares received
164
num_fed_shares_received.info current total of federated shares received
165
num_fed_shares_received.min 0
166
multigraph nextcloud_dbsize_$CLEANDOMAIN
167
graph_title Nextcloud database size on $DOMAIN
168
graph_args --base 1024 -l 0
169
graph_vlabel size in bytes
170
graph_info database database size in bytes
171
graph_category cloud
172
db_size.label database size in bytes
173
db_size.info database size in bytes
174
db_size.draw AREA
175
db_size.min 0
176
multigraph nextcloud_storages_$CLEANDOMAIN
177
graph_title Nextcloud storages on $DOMAIN
178
graph_args --base 1000 -l 0
179
graph_printf %.0lf
180
graph_vlabel number
181
graph_info number of storages
182
graph_category cloud
183
num_storages.label total number of storages
184
num_storages.info current total of storages
185
num_storages.min 0
186
num_storages_local.label number of local storages
187
num_storages_local.info current number of local storages
188
num_storages_local.min 0
189
num_storages_home.label number of home storages
190
num_storages_home.info current number of home storages
191
num_storages_home.min 0
192
num_storages_other.label number of other storages
193
num_storages_other.info current number of other storages
194
num_storages_other.min 0
195
multigraph nextcloud_apps_$CLEANDOMAIN
196
graph_title Nextcloud apps on $DOMAIN
197
graph_args --base 1000 -l 0
198
graph_printf %.0lf
199
graph_vlabel apps
200
graph_info number of installed and updatable apps
201
graph_category cloud
202
num_updates_available.label available app updates
203
num_updates_available.info number of available app updates
204
num_updates_available.min 0
205
num_updates_available.warning 1
206
num_installed.label installed apps
207
num_installed.info number of installed apps
208
num_installed.min 0
209
EOM
210
	exit 0
211
        ;;
212

    
213
esac
214

    
215
# Get JSON data
216
JSONSTATS=$(
217
	fetch_url -u "$USERNAME:$PASSWORD" "https://${DOMAIN}${API_PATH}" | sed 's/\\/\\\\/g' | jq -cr ".ocs.data" 2>&1 \
218
	|| fetch_url -u "$USERNAME:$PASSWORD" "http://${DOMAIN}${API_PATH}" | sed 's/\\/\\\\/g' | jq -cr ".ocs.data"
219
)
220
USERS=$(echo "$JSONSTATS" | jq -cr ".activeUsers")
221
STORAGE=$(echo "$JSONSTATS" | jq -cr ".nextcloud.storage")
222
SHARES=$(echo "$JSONSTATS" | jq -cr ".nextcloud.shares")
223
DBSIZE=$(echo "$JSONSTATS" | jq -cr ".server.database.size")
224
APPS=$(echo "$JSONSTATS" | jq -cr ".nextcloud.system.apps")
225

    
226
# users
227
echo "multigraph nextcloud_users_$CLEANDOMAIN"
228
print_json_data "$USERS" last5minutes last1hour last24hours
229
print_json_data "$STORAGE" num_users
230

    
231
# files
232
echo "multigraph nextcloud_files_$CLEANDOMAIN"
233
print_json_data "$STORAGE" num_files
234

    
235
# storages
236
echo "multigraph nextcloud_storages_$CLEANDOMAIN"
237
print_json_data "$STORAGE" num_storages num_storages_local num_storages_home num_storages_other
238

    
239
# shares
240
echo "multigraph nextcloud_shares_$CLEANDOMAIN"
241
print_json_data "$SHARES" num_shares num_shares_user num_shares_groups num_shares_link num_shares_mail num_shares_room num_shares_link_no_password num_fed_shares_sent num_fed_shares_received
242

    
243
# dbsize
244
echo "multigraph nextcloud_dbsize_$CLEANDOMAIN"
245
echo "db_size.value $DBSIZE"
246

    
247
# apps
248
echo "multigraph nextcloud_apps_$CLEANDOMAIN"
249
print_json_data "$APPS" num_installed num_updates_available