Improve script options
Benjamin Renard

Benjamin Renard commited on 2015-01-07 16:19:54
Showing 2 changed files, with 63 additions and 30 deletions.

... ...
@@ -4,9 +4,11 @@ Nagios plugin to check Git Repository status
4 4
 Usage
5 5
 -----
6 6
 
7
-  Usage : ./check_git_config [directory] [-d]
8
-          [directory]     Git root directory (default : /srv/common)
9
-          [-d]            Enable debug mode
7
+  Usage : ./check_git_config -g [directory] [-c|-r remote] [-d]
8
+          -g [directory]	Specify Git root directory (default : /srv/common)
9
+          -c		Check Git remote state
10
+          -r [remote]	Specify Git remote to check (default : origin)
11
+          -d		Enable debug mode
10 12
 
11 13
 Copyright
12 14
 ---------
... ...
@@ -1,40 +1,59 @@
1 1
 #!/bin/bash
2 2
 #
3
-# Nagios plugin to check Postgresql streamin replication state
4
-#
5
-# Could be use on Master or on standby node
6
-#
7
-# Requirement :
8
-#
9
-#     On master node : Slaves must be able to connect with user PG_USER
10
-#                      to database postgres as trust
11
-#
12
-#     On standby node : PG_USER must be able to connect localy as trust
3
+# Nagios plugin to check Git repository status
13 4
 #
14 5
 # Author : Benjamin Renard <brenard@easter-eggs.com>
15 6
 # Date : Wed, 14 Mar 2012 14:45:55 +0000
16
-# Source : http://git.zionetrix.net/check_pg_streaming_replication
7
+# Source : http://git.zionetrix.net/check_git_config
17 8
 #
18 9
 
19 10
 GIT_ROOT=/srv/common
11
+GIT_REMOTE=origin
12
+DEBUG=0
13
+CHECK_REMOTE=0
20 14
 
21
-if [ "$1" == "-h" ]
22
-then
23
-	echo "Usage : $0 [directory] [-d]
24
-	[directory]	Git root directory (default : $GIT_ROOT)
25
-	[-d]		Enable debug mode"
15
+function usage() {
16
+	echo "Usage : $0 -g [directory] [-c|-r remote] [-d]
17
+	-g [directory]	Specify Git root directory (default : $GIT_ROOT)
18
+	-c		Check Git remote state
19
+	-r [remote]	Specify Git remote to check (default : $GIT_REMOTE)
20
+	-d		Enable debug mode"
21
+}
22
+
23
+while getopts "g:r:cdh-:" OPTION
24
+do
25
+	case "$OPTION" in
26
+		c)
27
+			CHECK_REMOTE=1
28
+		;;
29
+		g)
30
+			GIT_ROOT="${OPTARG}"
31
+		;;
32
+		r)
33
+			CHECK_REMOTE=1
34
+			GIT_REMOTE="${OPTARG}"
35
+		;;
36
+		d)
37
+			DEBUG=1
38
+		;;
39
+		h)
40
+			usage
26 41
 			exit 0
27
-fi
42
+		;;
43
+		*)
44
+			echo "Invalid parameter -$OPTION"
45
+			echo
46
+			usage
47
+			exit 1
48
+		;;
49
+	esac
50
+done
28 51
 
29
-[ -n "$1" -a "$1" != "-d" ] && GIT_ROOT="$1"
30 52
 [ ! -d "$GIT_ROOT" ] && echo "UNKNOWN : Git root directory does not exists !" && exit 3
31 53
 [ ! -d "$GIT_ROOT/.git" ] && echo "UNKNOWN : Git root directory seem to not being a git repository." && exit 3
32 54
 
33 55
 cd $GIT_ROOT
34 56
 
35
-DEBUG=0
36
-[ "$1" == "-d" -o "$2" == "-d" ] && DEBUG=1
37
-
38 57
 STATUS=$( git status -s )
39 58
 
40 59
 [ $DEBUG -eq 1 ] && echo -e "Status : $STATUS"
... ...
@@ -43,11 +62,24 @@ if [ -n "$STATUS" ]
43 62
 then
44 63
 	echo "WARNING : Git config repo on $( hostname ) not clean"
45 64
 	exit 1
46
-else
65
+elif [ $CHECK_REMOTE -eq 1 ]
66
+then
67
+	# Check remote exists
68
+	[ $DEBUG -eq 1 ] && echo -n "Check remote '$GIT_REMOTE' exist : "
69
+	git remote show "$GIT_REMOTE" > /dev/null 2>&1
70
+	res=$?
71
+	[ $DEBUG -eq 1 ] && echo "done. (Return $res)"
72
+
73
+	if [ $res -ne 0 ]
74
+	then
75
+		echo "UNKNOWN : Unkown remote '$GIT_REMOTE'"
76
+		exit 3
77
+	fi
78
+
47 79
 	[ $DEBUG -eq 1 ] && echo -n "Fecth : "
48
-	git fetch > /dev/null 2>&1
80
+	git fetch "$GIT_REMOTE" > /dev/null 2>&1
49 81
 	res=$?
50
-	[ $DEBUG -eq 1 ] && echo "done. (Return $?)"
82
+	[ $DEBUG -eq 1 ] && echo "done. (Return $res)"
51 83
 
52 84
 	if [ $res -ne 0 ]
53 85
 	then
... ...
@@ -65,8 +97,7 @@ else
65 97
 	then
66 98
 		echo "CRITICAL : Git config not uptodate"
67 99
 		exit 2
68
-	else
69
-		echo "OK"
70
-		exit 0
71 100
 	fi
72 101
 fi
102
+echo "OK"
103
+exit 0
73 104