Browse Source

Splitting checks in multiple files

Yann Weber 11 months ago
parent
commit
b17b1318e8
9 changed files with 645 additions and 514 deletions
  1. 9
    514
      check.sh
  2. 28
    0
      checks/git.sh
  3. 107
    0
      checks/http.sh
  4. 65
    0
      checks/mail.sh
  5. 41
    0
      checks/mpd.sh
  6. 53
    0
      checks/net.sh
  7. 77
    0
      checks/ssh.sh
  8. 119
    0
      checks/webradio.sh
  9. 146
    0
      checks/xmpp.sh

+ 9
- 514
check.sh View File

@@ -1,6 +1,6 @@
1 1
 #!/bin/sh
2 2
 #check.sh : functionnal check functions for various server
3
-#Copyright (C) 2016 Weber Yann
3
+#Copyright (C) 2016,2023 Weber Yann
4 4
 #
5 5
 #This program is free software; you can redistribute it and/or modify
6 6
 #it under the terms of the GNU General Public License as published by
@@ -15,7 +15,6 @@
15 15
 #You should have received a copy of the GNU General Public License
16 16
 #along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 17
 
18
-
19 18
 if [ -z "$color" ]
20 19
 then
21 20
 	color=1
@@ -202,517 +201,13 @@ CHECK_REPORT() {
202 201
 	fi
203 202
 }
204 203
 
205
-#
206
-#	HTTP/HTTPS tests
207
-#
208
-
209
-_check_http_status() {
210
-	# $1 url
211
-	# $2 status
212
-	# $* curl options
213
-	url=$1
214
-	shift 1
215
-	expt=$1
216
-	if [ -z "$1" ]
217
-	then
218
-		expt=200
219
-	fi
220
-	shift 1
221
-
222
-	status=$(curl -s -o /dev/null -w "%{http_code}" "$@" "$url")
223
-	if [ "$status" -ne "$expt" ]
224
-	then
225
-		fail "Check http status $expt for $url : $status returned" 
226
-	elif [ "$verbose" -gt 0 ]
227
-	then
228
-		success "Check http status $status for $url"
229
-	fi
230
-}
231
-
232
-check_http_status() {
233
-	# $1 url
234
-	# $2 status
235
-	_check_http_status "$1" "$2"
236
-}
237
-
238
-check_http_200() {
239
-	_check_http_status "$1" 200
240
-}
241
-
242
-check_https_cert() {
243
-	# Check that SSL Cert is valid
244
-	# $1 URL
245
-	status=$(curl -s -o /dev/null -w "%{http_code}" "https://$1")
246
-	rep=$?
247
-	if [ "$rep" -eq 0 ]
248
-	then
249
-		success "https://$1 cert verified"
250
-		return
251
-	fi
252
-	status=$(curl -k -s -o /dev/null -q "%{http_code}" "https://$1")
253
-	rep=$?
254
-	if [ "$rep" -eq 0 ]
255
-	then
256
-		fail "https://$1 cert invalid"
257
-	else
258
-		err "Unable to curl https://$1"
259
-	fi
260
-}
261
-
262
-check_html_title() {
263
-	url="$1"
264
-	expt="$2"
265
-
266
-	tmpxsl=$(mktemp -t XSL.XXXXXXXXXXX)
267
-	echo '<?xml version="1.0"?>
268
-<xsl:stylesheet version="1.0"
269
-xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
270
-		<xsl:output method = "text"/>
271
-		<xsl:template match="/">
272
-				<xsl:value-of select="/html/head/title"/>
273
-		</xsl:template>
274
-</xsl:stylesheet>' > "$tmpxsl"
275
-
276
-	tmphtml=$(mktemp -t html.XXXXXXXXX)
277
-
278
-	curl --silent "$url" > "$tmphtml"
279
-	if title=$(xsltproc --html --novalid "$tmpxsl" "$tmphtml" 2>/dev/null)
280
-	then
281
-		:
282
-	else
283
-		title=$(xsltproc --novalid "$tmpxsl" "$tmphtml" 2>/dev/null)
284
-	fi
285
-
286
-	if [ "$title" = "$expt" ]
287
-	then
288
-		success "$url HTML title is '$expt'"
289
-	else
290
-		fail "$url HTML title is '$title' but '$expt' expected"
291
-	fi
292
-
293
-	rm "$tmpxsl" "$tmphtml" 2>/dev/null
294
-}
295
-
296
-check_audiostream() {
297
-	# Uses one of mplayer or mpv to retrieve a small amount of stream
298
-	# and attempt to decode it.
299
-	# $1 Stream URL
300
-
301
-	MPLAYER="$(which mplayer)"
302
-	MPV="$(which mpv)"
303
-	if [ -x "$MPLAYER" ]
304
-	then
305
-		check_audiostream_mplayer "$1"
306
-	elif [ -x "$MPV" ]
307
-	then
308
-		check_audiostream_mpv "$1"
309
-	else
310
-		fail "Unable to find mplayer nor mpv"
311
-	fi
312
-}
313
-
314
-check_audiostream_mplayer() {
315
-	# Uses mplayer to fetch 128Kb (by default) of stream
316
-	# $1 Stream URL
317
-	# $2 size in kb without the kb suffix
318
-	# $3 mplayer path
319
-
320
-	tmpfile=$(mktemp -t check_audiostream.XXXXXXXXX)
321
-	sz="$2"
322
-	if [ -z "$sz" ]
323
-	then
324
-		sz="128"
325
-	fi
326
-	MPLAYER="$3"
327
-	if [ -z "$MPLAYER" ]
328
-	then
329
-		MPLAYER="$(which mplayer)"
330
-	fi
331
-
332
-	sz_kb="${sz}kb"
333
-	expt_sz="$(( sz * 8 ))"
334
-
335
-	if [ "$verbose" -gt 1 ]
336
-	then
337
-		logdate INFO "$tc_name: Running mplayer on '$1' for $sz_kb" 3
338
-	fi
339
-
340
-	$MPLAYER -endpos "$sz_kb" -ao "pcm:file=$tmpfile" "$1" 1>/dev/null 2>/dev/null
341
-	res="$?"
342
-	bytes=$(du -b "$tmpfile" | cut -f1)
343
-	rm "$tmpfile" 2>/dev/null
344
-
345
-	if [ "$bytes" -lt $expt_sz ]
346
-	then
347
-		fail "mplayer retrieved ${bytes}B of stream instead of expected $sz_kb"
348
-		return
349
-	fi
350
-	if [ "$res" -eq 0 ]
351
-	then
352
-		success "mplayer retrieved $sz of stream on $1"
353
-	else
354
-		fail "mplayer failed to retrieve stream on $1"
355
-	fi
356
-}
357
-
358
-check_audiostream_mpv() {
359
-	# Uses mpv to fetch 4s of audio stream
360
-	# $1 Stream URL
361
-	# $2 time of stream to fetch
362
-	# $3 mpv path
363
-
364
-	time="$2"
365
-	if [ -z "$time" ]
366
-	then
367
-		time="4"
368
-	fi
369
-	MPV="$3"
370
-	if [ -z "$MPV" ]
371
-	then
372
-		MPV="$(which mpv)"
373
-	fi
374
-
375
-
376
-	if [ "$verbose" -gt 1 ]
377
-	then
378
-		logdate INFO "$tc_name: Running mpv on '$1' for ${time}s" 3
379
-	fi
380
-	start_time=$(date "+%s")
381
-	$MPV --vo=null --ao=null --o=/dev/null --of=wav --length $time "$1" 1> /dev/null 2>/dev/null
382
-	res=$?
383
-	stop_time=$(date "+%s")
384
-	run_time=$(( stop_time - start_time))
385
-
386
-	if [ "$run_time" -lt "$time" ]
387
-	then
388
-		fail "mpv stopped running ${run_time} after launch but ${time}s should be received"
389
-		return
390
-	fi
391
-	
392
-	if [ "$res" -eq 0 ]
393
-	then
394
-		success "mpv retrieved ${time}s of stream on $1"
395
-	else
396
-		fail "mpv failed to retrieve ${time}s of stream on $1"
397
-	fi
398
-}
399
-
400
-check_ping() {
401
-	hostname=$1
402
-	count=$2
403
-	proto="$3"
404
-	if [ -z "$count" ]
405
-	then
406
-		count=5
407
-	fi
408
-	case "$proto" in
409
-		ipv4)
410
-			protoping="ping -4";;
411
-		ipv6)
412
-			protoping="ping -6";;
413
-		*)
414
-			protoping="ping";;
415
-	esac
416
-
417
-	if $protoping -i 0.2 -c "$count" "$hostname" >/dev/null
418
-	then
419
-		success "successfully send $count ping to '$hostname'"
420
-	else
421
-		fail "unable to ping '$hostname'"
422
-	fi
423
-}
424
-
425
-check_dns() {
426
-	hostname=$1
427
-	field=$2
428
-	expt=$3
429
-	result=$(dig +short "$hostname" "$field")
430
-	if echo "$result" | grep -e "$expt" >/dev/null
431
-	then
432
-		success "DNS replied '$expt' for '$field $hostname'"
433
-	else
434
-		fail "Unexpected DNS reply '$result' expecting '$expt' for '$hostname $field'"
435
-	fi
436
-}
437
-
438
-check_smtp() {
439
-	hostname=$1
440
-	port=$2
441
-	if [ -z "$port" ]
442
-	then
443
-		port=587
444
-	fi
445
-	code=$(nc -q0 "$hostname" "$port" < /dev/null | cut -d " " -f1)
446
-	if [ "$code" = "220" ]
447
-	then
448
-		success "SMTP@$hostname server replied 220 status"
449
-	else
450
-		fail "Unexpected reply from smtp@$hostname '$code'"
451
-	fi
452
-}
453
-
454
-check_smtp_id() {
455
-	hostname=$1
456
-	idstr=$2
457
-	port=$3
458
-	if [ -z "$port" ]
459
-	then
460
-		port=587
461
-	fi
462
-	rep=$(nc -q0 "$hostname" "$port" < /dev/null)
463
-	if echo "$rep" | grep -e "^220 $idstr" > /dev/null
464
-	then
465
-		success "SMTP@$hostname server replie '$idstr'"
466
-	else
467
-		fail "Unexpected reply from smtp@$hostname '$rep'"
468
-	fi
469
-}
470
-
471
-check_imaps() {
472
-	hostname=$1
473
-	port=$2
474
-	if [ -z "$port" ]
475
-	then
476
-		port=993
477
-	fi
478
-	rep=$(echo "01 LOGOUT" | openssl s_client -quiet -verify_quiet -connect "${hostname}:$port" -servername "$hostname")
479
-
480
-	if echo "$rep" | grep "^01 OK" > /dev/null
481
-	then
482
-		success "IMAPS@$hostname '$(echo "$rep" | grep "^01 OK")'"
483
-	else
484
-		fail "Unexpected reply from imaps@$hostname '$rep'"
485
-	fi
486
-}
487
-
488
-check_git_repo() {
489
-	tmpdir=$(mktemp -d -t check_git.XXXXXXXXX)
490
-	git clone "$1" "$tmpdir" 2>/dev/null 1>/dev/null
491
-	res="$?"
492
-	rm -Rf "$tmpdir"
493
-	if [ "$res" -ne 0 ]
494
-	then
495
-		fail "unable to clone git repo '$1'"
496
-	else
497
-		success "git repo '$1' cloned'"
498
-	fi
499
-}
500
-
501
-#
502
-# Jabber XMPP checks
503
-#
504
-
505
-__xmpp_probe() {
506
-	serv=$1
507
-	timeout=$2
508
-	type=$3
509
-
510
-	payload=$(printf "%s" "<?xml version=\"1.0\"?>\n<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:$type\" to=\"${1}\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">\n")
511
-	if [ "$verbose" -gt 2 ]
512
-	then
513
-		echo "$payload" | sed -e "s/^/$(datetime) [  DEBUG] Sent : /" >&2
514
-	fi
515
-	echo "$payload"
516
-	sleep "$timeout"
517
-}
518
-
519
-_xmpp_probe() {
520
-	serv=$1
521
-	port=$2
522
-	timeout=$3
523
-	type=$4
524
-
525
-	tmpres=$(mktemp -t xmpp_probe.XXXXXXXXX)
526
-
527
-	echo "$(__xmpp_probe "$serv" "$timeout" "$type"| nc -q2 "$serv" "$port")</stream:stream>" | xmllint --format - > "$tmpres"
528
-	if [ "$verbose" -gt 2 ]
529
-	then
530
-		sed -e "s/^/$(datetime) [  DEBUG] Recv : /" >&2 < "$tmpres"
531
-	fi
532
-	cat "$tmpres"
533
-	rm "$tmpres"
534
-}
535
-
536
-_check_xmpp_ns() {
537
-	ns1=$1
538
-	expt_ns=$2
539
-	expt_port=$3
540
-	dnsq="_xmpp-${4}._tcp.${ns1}"
541
-	rep="$(dig "$dnsq" srv +short | cut -d" " -f3,4)"
542
-	if [ "$rep" = "$expt_port ${expt_ns}." ]
543
-	then
544
-		success "$dnsq = '$rep'"
545
-	else
546
-		fail "$dnsq = '$rep' but '$expt_port ${expt_ns}.' expected"
547
-	fi
548
-}
549
-
550
-check_xmpp_serv_ns() {
551
-	_check_xmpp_ns "$1" "$2" "$3" "server"
552
-}
553
-
554
-check_xmpp_client_ns() {
555
-	_check_xmpp_ns "$1" "$2" "$3" "client"
556
-}
557
-
558
-_check_xmpp_server() {
559
-	serv=$1
560
-	port=$2
561
-	timeout=$3
562
-	type=$4
563
-	
564
-	if [ -z "$port" ]
565
-	then
566
-		port=5222
567
-	fi
568
-	if [ -z "$timeout" ]
569
-	then
570
-		timeout=2
571
-	fi
572
-
573
-	if [ "$verbose" -gt 1 ]
574
-	then
575
-		tpe="client"
576
-		if [ "$type" = "server" ]
577
-		then
578
-			tpe="S2S"
579
-		fi
580
-		logdate INFO "$tc_name: Connecting to XMPP $serv $tpe port $port (timeout=${timeout}s)" 3
581
-	fi
582
-
583
-	stream=$(_xmpp_probe "$serv" "$port" "$timeout" "$type"| head -n2 | tail -n1)
584
-	if [ -z "$stream" ]
585
-	then
586
-		fail "Empty reply from $serv:$port"
587
-		return
588
-	fi
589
-
590
-	if [ "$type" = "client" ]
591
-	then
592
-		infos=$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)".* from="([^"]+)" .*$/\1 \2 \3/')
593
-	else
594
-		infos="$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)" .*$/\1 \2/') $serv"
595
-	fi
596
-
597
-	if echo "$infos" | grep "jabber:$type" >/dev/null
598
-	then
599
-		success "Successfully connected to XMPP $type $serv:$port"
600
-	else
601
-		fail "Unexpected reply from $serv:$port : $infos"
602
-	fi
603
-}
604
-
605
-check_xmpp_server_client() {
606
-	_check_xmpp_server "$1" "$2" "$3" "client"
607
-}
608
-
609
-check_xmpp_server_s2s() {
610
-	_check_xmpp_server "$1" "$2" "$3" "server"
611
-}
612
-
613
-check_xmpp_ssl() {
614
-	serv=$1
615
-	port=$2
616
-	
617
-	if [ -z "$port" ]
618
-	then
619
-		port=5222
620
-	fi
621
-
622
-	openssl s_client -connect "$serv:$port" </dev/null -starttls xmpp >/dev/null 2>/dev/null
623
-	rep=$?
624
-	if [ "$rep" -eq 0 ]
625
-	then
626
-		success "Openssl successfully negociating XMPP ssl with $serv:$port"
627
-	else
628
-		fail "Openssl failed negociating XMPP ssl with $serv:$port"
629
-	fi
630
-}
631
-
632
-#
633
-#	SSH checks
634
-#
635
-
636
-check_ssh_nc() {
637
-	host=$1
638
-	port=$2
639
-	
640
-	if [ -z "$port" ]
641
-	then
642
-		port=22
643
-	fi
644 204
 
645
-	rep="$(nc -w1 "$host" "$port" </dev/null)"
646
-	res=$?
647
-	if [ "$res" -ne "0" ]
648
-	then
649
-		fail "Netcat unable to connect to $host:$port"
650
-		return
651
-	fi
652
-	if echo "$rep" | grep "^SSH-2.0-OpenSSH" >/dev/null
653
-	then
654
-		success "OpenSSH replied on $host:$port"
655
-	else
656
-		fail "Bad reply from $host:$port : '$rep'"
657
-	fi
658
-}
205
+. ./checks/git.sh
206
+. ./checks/http.sh
207
+. ./checks/mail.sh
208
+. ./checks/mpd.sh
209
+. ./checks/net.sh
210
+. ./checks/ssh.sh
211
+. ./checks/webradio.sh
212
+. ./checks/xmpp.sh
659 213
 
660
-check_ssh_key() {
661
-	host="$1"
662
-	testkey="$2"
663
-	keytype="$3"
664
-	port="$4"
665
-	
666
-	if [ -z "$port" ]
667
-	then
668
-		port=22
669
-	fi
670
-
671
-	if [ -z "$keytype" ]
672
-	then
673
-		keytype="rsa"
674
-	fi
675
-
676
-	key=$(ssh-keyscan -p $port -t "$keytype" "$host" 2>/dev/null | cut -d " " -f3)
677
-
678
-	if [ -z "$key" ]
679
-	then
680
-		fail "SSH server not responding"
681
-		return
682
-	elif [ "$key" = "$testkey" ]
683
-	then
684
-		success "OpenSSH $host:$port key is $testkey"
685
-		return
686
-	else
687
-		fail "OpenSSH $host:$port missmatch : "
688
-		logdate ERR "Expected : $testkey" 1
689
-		logdate ERR "Received : $key" 1
690
-		return
691
-	fi
692
-}
693
-
694
-check_mpc() {
695
-	# check_mpc [$host [$port] ]
696
-	# tests if you can contact an MPD server in a client way
697
-	# returns the current state of the server
698
-
699
-	host=$1
700
-	port=$2
701
-
702
-	if [ -z "$host" ]
703
-	then
704
-		host=127.0.0.1
705
-	fi
706
-
707
-	if [ -z "$port" ]
708
-	then
709
-		port=6600
710
-	fi
711
-
712
-	if echo "status" | nc -w 1 "$host" "$port" | head -1 | grep "^OK MPD"
713
-	then
714
-		success "MPD server can be contacted on $host:$port"
715
-		status=$(echo "status" | nc -w 1 "$host" "$port" | grep "^state:"|cut -d: -f2)
716
-		success "It's state is : $status"
717
-	fi
718
-}

+ 28
- 0
checks/git.sh View File

@@ -0,0 +1,28 @@
1
+#Copyright (C) 2016,2023 Weber Yann
2
+#
3
+#This program is free software; you can redistribute it and/or modify
4
+#it under the terms of the GNU General Public License as published by
5
+#the Free Software Foundation; either version 3 of the License, or
6
+#any later version.
7
+#
8
+#This program is distributed in the hope that it will be useful,
9
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+#GNU General Public License for more details.
12
+#
13
+#You should have received a copy of the GNU General Public License
14
+#along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+
16
+check_git_repo() {
17
+	tmpdir=$(mktemp -d -t check_git.XXXXXXXXX)
18
+	git clone "$1" "$tmpdir" 2>/dev/null 1>/dev/null
19
+	res="$?"
20
+	rm -Rf "$tmpdir"
21
+	if [ "$res" -ne 0 ]
22
+	then
23
+		fail "unable to clone git repo '$1'"
24
+	else
25
+		success "git repo '$1' cloned'"
26
+	fi
27
+}
28
+

+ 107
- 0
checks/http.sh View File

@@ -0,0 +1,107 @@
1
+#Copyright (C) 2016,2023 Weber Yann
2
+#
3
+#This program is free software; you can redistribute it and/or modify
4
+#it under the terms of the GNU General Public License as published by
5
+#the Free Software Foundation; either version 3 of the License, or
6
+#any later version.
7
+#
8
+#This program is distributed in the hope that it will be useful,
9
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+#GNU General Public License for more details.
12
+#
13
+#You should have received a copy of the GNU General Public License
14
+#along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+
16
+
17
+#
18
+#	HTTP/HTTPS tests
19
+#
20
+
21
+_check_http_status() {
22
+	# $1 url
23
+	# $2 status
24
+	# $* curl options
25
+	url=$1
26
+	shift 1
27
+	expt=$1
28
+	if [ -z "$1" ]
29
+	then
30
+		expt=200
31
+	fi
32
+	shift 1
33
+
34
+	status=$(curl -s -o /dev/null -w "%{http_code}" "$@" "$url")
35
+	if [ "$status" -ne "$expt" ]
36
+	then
37
+		fail "Check http status $expt for $url : $status returned" 
38
+	elif [ "$verbose" -gt 0 ]
39
+	then
40
+		success "Check http status $status for $url"
41
+	fi
42
+}
43
+
44
+check_http_status() {
45
+	# $1 url
46
+	# $2 status
47
+	_check_http_status "$1" "$2"
48
+}
49
+
50
+check_http_200() {
51
+	_check_http_status "$1" 200
52
+}
53
+
54
+check_https_cert() {
55
+	# Check that SSL Cert is valid
56
+	# $1 URL
57
+	status=$(curl -s -o /dev/null -w "%{http_code}" "https://$1")
58
+	rep=$?
59
+	if [ "$rep" -eq 0 ]
60
+	then
61
+		success "https://$1 cert verified"
62
+		return
63
+	fi
64
+	status=$(curl -k -s -o /dev/null -q "%{http_code}" "https://$1")
65
+	rep=$?
66
+	if [ "$rep" -eq 0 ]
67
+	then
68
+		fail "https://$1 cert invalid"
69
+	else
70
+		err "Unable to curl https://$1"
71
+	fi
72
+}
73
+
74
+check_html_title() {
75
+	url="$1"
76
+	expt="$2"
77
+
78
+	tmpxsl=$(mktemp -t XSL.XXXXXXXXXXX)
79
+	echo '<?xml version="1.0"?>
80
+<xsl:stylesheet version="1.0"
81
+xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
82
+		<xsl:output method = "text"/>
83
+		<xsl:template match="/">
84
+				<xsl:value-of select="/html/head/title"/>
85
+		</xsl:template>
86
+</xsl:stylesheet>' > "$tmpxsl"
87
+
88
+	tmphtml=$(mktemp -t html.XXXXXXXXX)
89
+
90
+	curl --silent "$url" > "$tmphtml"
91
+	if title=$(xsltproc --html --novalid "$tmpxsl" "$tmphtml" 2>/dev/null)
92
+	then
93
+		:
94
+	else
95
+		title=$(xsltproc --novalid "$tmpxsl" "$tmphtml" 2>/dev/null)
96
+	fi
97
+
98
+	if [ "$title" = "$expt" ]
99
+	then
100
+		success "$url HTML title is '$expt'"
101
+	else
102
+		fail "$url HTML title is '$title' but '$expt' expected"
103
+	fi
104
+
105
+	rm "$tmpxsl" "$tmphtml" 2>/dev/null
106
+}
107
+

+ 65
- 0
checks/mail.sh View File

@@ -0,0 +1,65 @@
1
+#Copyright (C) 2016,2023 Weber Yann
2
+#
3
+#This program is free software; you can redistribute it and/or modify
4
+#it under the terms of the GNU General Public License as published by
5
+#the Free Software Foundation; either version 3 of the License, or
6
+#any later version.
7
+#
8
+#This program is distributed in the hope that it will be useful,
9
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+#GNU General Public License for more details.
12
+#
13
+#You should have received a copy of the GNU General Public License
14
+#along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+
16
+check_smtp() {
17
+	hostname=$1
18
+	port=$2
19
+	if [ -z "$port" ]
20
+	then
21
+		port=587
22
+	fi
23
+	code=$(nc -q0 "$hostname" "$port" < /dev/null | cut -d " " -f1)
24
+	if [ "$code" = "220" ]
25
+	then
26
+		success "SMTP@$hostname server replied 220 status"
27
+	else
28
+		fail "Unexpected reply from smtp@$hostname '$code'"
29
+	fi
30
+}
31
+
32
+check_smtp_id() {
33
+	hostname=$1
34
+	idstr=$2
35
+	port=$3
36
+	if [ -z "$port" ]
37
+	then
38
+		port=587
39
+	fi
40
+	rep=$(nc -q0 "$hostname" "$port" < /dev/null)
41
+	if echo "$rep" | grep -e "^220 $idstr" > /dev/null
42
+	then
43
+		success "SMTP@$hostname server replie '$idstr'"
44
+	else
45
+		fail "Unexpected reply from smtp@$hostname '$rep'"
46
+	fi
47
+}
48
+
49
+check_imaps() {
50
+	hostname=$1
51
+	port=$2
52
+	if [ -z "$port" ]
53
+	then
54
+		port=993
55
+	fi
56
+	rep=$(echo "01 LOGOUT" | openssl s_client -quiet -verify_quiet -connect "${hostname}:$port" -servername "$hostname")
57
+
58
+	if echo "$rep" | grep "^01 OK" > /dev/null
59
+	then
60
+		success "IMAPS@$hostname '$(echo "$rep" | grep "^01 OK")'"
61
+	else
62
+		fail "Unexpected reply from imaps@$hostname '$rep'"
63
+	fi
64
+}
65
+

+ 41
- 0
checks/mpd.sh View File

@@ -0,0 +1,41 @@
1
+#Copyright (C) 2016,2023 Weber Yann
2
+#
3
+#This program is free software; you can redistribute it and/or modify
4
+#it under the terms of the GNU General Public License as published by
5
+#the Free Software Foundation; either version 3 of the License, or
6
+#any later version.
7
+#
8
+#This program is distributed in the hope that it will be useful,
9
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+#GNU General Public License for more details.
12
+#
13
+#You should have received a copy of the GNU General Public License
14
+#along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+
16
+check_mpc() {
17
+	# check_mpc [$host [$port] ]
18
+	# tests if you can contact an MPD server in a client way
19
+	# returns the current state of the server
20
+
21
+	host=$1
22
+	port=$2
23
+
24
+	if [ -z "$host" ]
25
+	then
26
+		host=127.0.0.1
27
+	fi
28
+
29
+	if [ -z "$port" ]
30
+	then
31
+		port=6600
32
+	fi
33
+
34
+	if echo "status" | nc -w 1 "$host" "$port" | head -1 | grep "^OK MPD"
35
+	then
36
+		success "MPD server can be contacted on $host:$port"
37
+		status=$(echo "status" | nc -w 1 "$host" "$port" | grep "^state:"|cut -d: -f2)
38
+		success "It's state is : $status"
39
+	fi
40
+}
41
+

+ 53
- 0
checks/net.sh View File

@@ -0,0 +1,53 @@
1
+#Copyright (C) 2016,2023 Weber Yann
2
+#
3
+#This program is free software; you can redistribute it and/or modify
4
+#it under the terms of the GNU General Public License as published by
5
+#the Free Software Foundation; either version 3 of the License, or
6
+#any later version.
7
+#
8
+#This program is distributed in the hope that it will be useful,
9
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+#GNU General Public License for more details.
12
+#
13
+#You should have received a copy of the GNU General Public License
14
+#along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+
16
+check_ping() {
17
+	hostname=$1
18
+	count=$2
19
+	proto="$3"
20
+	if [ -z "$count" ]
21
+	then
22
+		count=5
23
+	fi
24
+	case "$proto" in
25
+		ipv4)
26
+			protoping="ping -4";;
27
+		ipv6)
28
+			protoping="ping -6";;
29
+		*)
30
+			protoping="ping";;
31
+	esac
32
+
33
+	if $protoping -i 0.2 -c "$count" "$hostname" >/dev/null
34
+	then
35
+		success "successfully send $count ping to '$hostname'"
36
+	else
37
+		fail "unable to ping '$hostname'"
38
+	fi
39
+}
40
+
41
+check_dns() {
42
+	hostname=$1
43
+	field=$2
44
+	expt=$3
45
+	result=$(dig +short "$hostname" "$field")
46
+	if echo "$result" | grep -e "$expt" >/dev/null
47
+	then
48
+		success "DNS replied '$expt' for '$field $hostname'"
49
+	else
50
+		fail "Unexpected DNS reply '$result' expecting '$expt' for '$hostname $field'"
51
+	fi
52
+}
53
+

+ 77
- 0
checks/ssh.sh View File

@@ -0,0 +1,77 @@
1
+#Copyright (C) 2016,2023 Weber Yann
2
+#
3
+#This program is free software; you can redistribute it and/or modify
4
+#it under the terms of the GNU General Public License as published by
5
+#the Free Software Foundation; either version 3 of the License, or
6
+#any later version.
7
+#
8
+#This program is distributed in the hope that it will be useful,
9
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+#GNU General Public License for more details.
12
+#
13
+#You should have received a copy of the GNU General Public License
14
+#along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+
16
+#
17
+#	SSH checks
18
+#
19
+
20
+check_ssh_nc() {
21
+	host=$1
22
+	port=$2
23
+	
24
+	if [ -z "$port" ]
25
+	then
26
+		port=22
27
+	fi
28
+
29
+	rep="$(nc -w1 "$host" "$port" </dev/null)"
30
+	res=$?
31
+	if [ "$res" -ne "0" ]
32
+	then
33
+		fail "Netcat unable to connect to $host:$port"
34
+		return
35
+	fi
36
+	if echo "$rep" | grep "^SSH-2.0-OpenSSH" >/dev/null
37
+	then
38
+		success "OpenSSH replied on $host:$port"
39
+	else
40
+		fail "Bad reply from $host:$port : '$rep'"
41
+	fi
42
+}
43
+
44
+check_ssh_key() {
45
+	host="$1"
46
+	testkey="$2"
47
+	keytype="$3"
48
+	port="$4"
49
+	
50
+	if [ -z "$port" ]
51
+	then
52
+		port=22
53
+	fi
54
+
55
+	if [ -z "$keytype" ]
56
+	then
57
+		keytype="rsa"
58
+	fi
59
+
60
+	key=$(ssh-keyscan -p $port -t "$keytype" "$host" 2>/dev/null | cut -d " " -f3)
61
+
62
+	if [ -z "$key" ]
63
+	then
64
+		fail "SSH server not responding"
65
+		return
66
+	elif [ "$key" = "$testkey" ]
67
+	then
68
+		success "OpenSSH $host:$port key is $testkey"
69
+		return
70
+	else
71
+		fail "OpenSSH $host:$port missmatch : "
72
+		logdate ERR "Expected : $testkey" 1
73
+		logdate ERR "Received : $key" 1
74
+		return
75
+	fi
76
+}
77
+

+ 119
- 0
checks/webradio.sh View File

@@ -0,0 +1,119 @@
1
+#copyright (c) 2016,2023 weber yann
2
+#
3
+#this program is free software; you can redistribute it and/or modify
4
+#it under the terms of the gnu general public license as published by
5
+#the free software foundation; either version 3 of the license, or
6
+#any later version.
7
+#
8
+#this program is distributed in the hope that it will be useful,
9
+#but without any warranty; without even the implied warranty of
10
+#merchantability or fitness for a particular purpose.  see the
11
+#gnu general public license for more details.
12
+#
13
+#you should have received a copy of the gnu general public license
14
+#along with this program.  if not, see <http://www.gnu.org/licenses/>.
15
+
16
+check_audiostream() {
17
+	# Uses one of mplayer or mpv to retrieve a small amount of stream
18
+	# and attempt to decode it.
19
+	# $1 Stream URL
20
+
21
+	MPLAYER="$(which mplayer)"
22
+	MPV="$(which mpv)"
23
+	if [ -x "$MPLAYER" ]
24
+	then
25
+		check_audiostream_mplayer "$1"
26
+	elif [ -x "$MPV" ]
27
+	then
28
+		check_audiostream_mpv "$1"
29
+	else
30
+		fail "Unable to find mplayer nor mpv"
31
+	fi
32
+}
33
+
34
+check_audiostream_mplayer() {
35
+	# Uses mplayer to fetch 128Kb (by default) of stream
36
+	# $1 Stream URL
37
+	# $2 size in kb without the kb suffix
38
+	# $3 mplayer path
39
+
40
+	tmpfile=$(mktemp -t check_audiostream.XXXXXXXXX)
41
+	sz="$2"
42
+	if [ -z "$sz" ]
43
+	then
44
+		sz="128"
45
+	fi
46
+	MPLAYER="$3"
47
+	if [ -z "$MPLAYER" ]
48
+	then
49
+		MPLAYER="$(which mplayer)"
50
+	fi
51
+
52
+	sz_kb="${sz}kb"
53
+	expt_sz="$(( sz * 8 ))"
54
+
55
+	if [ "$verbose" -gt 1 ]
56
+	then
57
+		logdate INFO "$tc_name: Running mplayer on '$1' for $sz_kb" 3
58
+	fi
59
+
60
+	$MPLAYER -endpos "$sz_kb" -ao "pcm:file=$tmpfile" "$1" 1>/dev/null 2>/dev/null
61
+	res="$?"
62
+	bytes=$(du -b "$tmpfile" | cut -f1)
63
+	rm "$tmpfile" 2>/dev/null
64
+
65
+	if [ "$bytes" -lt $expt_sz ]
66
+	then
67
+		fail "mplayer retrieved ${bytes}B of stream instead of expected $sz_kb"
68
+		return
69
+	fi
70
+	if [ "$res" -eq 0 ]
71
+	then
72
+		success "mplayer retrieved $sz of stream on $1"
73
+	else
74
+		fail "mplayer failed to retrieve stream on $1"
75
+	fi
76
+}
77
+
78
+check_audiostream_mpv() {
79
+	# Uses mpv to fetch 4s of audio stream
80
+	# $1 Stream URL
81
+	# $2 time of stream to fetch
82
+	# $3 mpv path
83
+
84
+	time="$2"
85
+	if [ -z "$time" ]
86
+	then
87
+		time="4"
88
+	fi
89
+	MPV="$3"
90
+	if [ -z "$MPV" ]
91
+	then
92
+		MPV="$(which mpv)"
93
+	fi
94
+
95
+
96
+	if [ "$verbose" -gt 1 ]
97
+	then
98
+		logdate INFO "$tc_name: Running mpv on '$1' for ${time}s" 3
99
+	fi
100
+	start_time=$(date "+%s")
101
+	$MPV --vo=null --ao=null --o=/dev/null --of=wav --length $time "$1" 1> /dev/null 2>/dev/null
102
+	res=$?
103
+	stop_time=$(date "+%s")
104
+	run_time=$(( stop_time - start_time))
105
+
106
+	if [ "$run_time" -lt "$time" ]
107
+	then
108
+		fail "mpv stopped running ${run_time} after launch but ${time}s should be received"
109
+		return
110
+	fi
111
+	
112
+	if [ "$res" -eq 0 ]
113
+	then
114
+		success "mpv retrieved ${time}s of stream on $1"
115
+	else
116
+		fail "mpv failed to retrieve ${time}s of stream on $1"
117
+	fi
118
+}
119
+

+ 146
- 0
checks/xmpp.sh View File

@@ -0,0 +1,146 @@
1
+#Copyright (C) 2016,2023 Weber Yann
2
+#
3
+#This program is free software; you can redistribute it and/or modify
4
+#it under the terms of the GNU General Public License as published by
5
+#the Free Software Foundation; either version 3 of the License, or
6
+#any later version.
7
+#
8
+#This program is distributed in the hope that it will be useful,
9
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+#GNU General Public License for more details.
12
+#
13
+#You should have received a copy of the GNU General Public License
14
+#along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+
16
+#
17
+# Jabber XMPP checks
18
+#
19
+
20
+__xmpp_probe() {
21
+	serv=$1
22
+	timeout=$2
23
+	type=$3
24
+
25
+	payload=$(printf "%s" "<?xml version=\"1.0\"?>\n<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:$type\" to=\"${1}\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">\n")
26
+	if [ "$verbose" -gt 2 ]
27
+	then
28
+		echo "$payload" | sed -e "s/^/$(datetime) [  DEBUG] Sent : /" >&2
29
+	fi
30
+	echo "$payload"
31
+	sleep "$timeout"
32
+}
33
+
34
+_xmpp_probe() {
35
+	serv=$1
36
+	port=$2
37
+	timeout=$3
38
+	type=$4
39
+
40
+	tmpres=$(mktemp -t xmpp_probe.XXXXXXXXX)
41
+
42
+	echo "$(__xmpp_probe "$serv" "$timeout" "$type"| nc -q2 "$serv" "$port")</stream:stream>" | xmllint --format - > "$tmpres"
43
+	if [ "$verbose" -gt 2 ]
44
+	then
45
+		sed -e "s/^/$(datetime) [  DEBUG] Recv : /" >&2 < "$tmpres"
46
+	fi
47
+	cat "$tmpres"
48
+	rm "$tmpres"
49
+}
50
+
51
+_check_xmpp_ns() {
52
+	ns1=$1
53
+	expt_ns=$2
54
+	expt_port=$3
55
+	dnsq="_xmpp-${4}._tcp.${ns1}"
56
+	rep="$(dig "$dnsq" srv +short | cut -d" " -f3,4)"
57
+	if [ "$rep" = "$expt_port ${expt_ns}." ]
58
+	then
59
+		success "$dnsq = '$rep'"
60
+	else
61
+		fail "$dnsq = '$rep' but '$expt_port ${expt_ns}.' expected"
62
+	fi
63
+}
64
+
65
+check_xmpp_serv_ns() {
66
+	_check_xmpp_ns "$1" "$2" "$3" "server"
67
+}
68
+
69
+check_xmpp_client_ns() {
70
+	_check_xmpp_ns "$1" "$2" "$3" "client"
71
+}
72
+
73
+_check_xmpp_server() {
74
+	serv=$1
75
+	port=$2
76
+	timeout=$3
77
+	type=$4
78
+	
79
+	if [ -z "$port" ]
80
+	then
81
+		port=5222
82
+	fi
83
+	if [ -z "$timeout" ]
84
+	then
85
+		timeout=2
86
+	fi
87
+
88
+	if [ "$verbose" -gt 1 ]
89
+	then
90
+		tpe="client"
91
+		if [ "$type" = "server" ]
92
+		then
93
+			tpe="S2S"
94
+		fi
95
+		logdate INFO "$tc_name: Connecting to XMPP $serv $tpe port $port (timeout=${timeout}s)" 3
96
+	fi
97
+
98
+	stream=$(_xmpp_probe "$serv" "$port" "$timeout" "$type"| head -n2 | tail -n1)
99
+	if [ -z "$stream" ]
100
+	then
101
+		fail "Empty reply from $serv:$port"
102
+		return
103
+	fi
104
+
105
+	if [ "$type" = "client" ]
106
+	then
107
+		infos=$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)".* from="([^"]+)" .*$/\1 \2 \3/')
108
+	else
109
+		infos="$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)" .*$/\1 \2/') $serv"
110
+	fi
111
+
112
+	if echo "$infos" | grep "jabber:$type" >/dev/null
113
+	then
114
+		success "Successfully connected to XMPP $type $serv:$port"
115
+	else
116
+		fail "Unexpected reply from $serv:$port : $infos"
117
+	fi
118
+}
119
+
120
+check_xmpp_server_client() {
121
+	_check_xmpp_server "$1" "$2" "$3" "client"
122
+}
123
+
124
+check_xmpp_server_s2s() {
125
+	_check_xmpp_server "$1" "$2" "$3" "server"
126
+}
127
+
128
+check_xmpp_ssl() {
129
+	serv=$1
130
+	port=$2
131
+	
132
+	if [ -z "$port" ]
133
+	then
134
+		port=5222
135
+	fi
136
+
137
+	openssl s_client -connect "$serv:$port" </dev/null -starttls xmpp >/dev/null 2>/dev/null
138
+	rep=$?
139
+	if [ "$rep" -eq 0 ]
140
+	then
141
+		success "Openssl successfully negociating XMPP ssl with $serv:$port"
142
+	else
143
+		fail "Openssl failed negociating XMPP ssl with $serv:$port"
144
+	fi
145
+}
146
+

Loading…
Cancel
Save