forked from HariSekhon/Nagios-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_git_branch_checkout.pl
executable file
·66 lines (56 loc) · 1.92 KB
/
check_git_branch_checkout.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl -T
# nagios: -epn
#
# Author: Hari Sekhon
# Date: 2012-07-26 12:09:53 +0100 (Thu, 26 Jul 2012)
#
# http://github.com/harisekhon/nagios-plugins
#
# License: see accompanying LICENSE file
#
$DESCRIPTION = "Nagios Plugin to check a Git working copy is in the right branch.
Primarily written for puppetmasters to make sure prod and staging
environment dirs had the right branches checked out in them";
$VERSION = "0.2";
use strict;
use warnings;
BEGIN {
use File::Basename;
use lib dirname(__FILE__) . "/lib";
}
use HariSekhonUtils;
my $directory;
my $branch;
my $branch_checkout;
my $git_default = "git";
my $git = $git_default;
%options = (
"d|directory=s" => [ \$directory, "Directory path to git working copy" ],
"b|branch=s" => [ \$branch, "Branch to expect working copy checkout to be" ],
"git-binary=s" => [ \$git, "Path to git binary. Defaults to '$git_default'. Without relative or fully qualified path to binary will use \$PATH" ],
);
@usage_order = qw/directory branch/;
get_options();
$directory = validate_directory($directory);
$branch or usage "branch name not specified";
$branch =~ /^([\w-]+)$/ or usage "Invalid branch name given, must be alpha-numeric";
$branch = $1;
$git = validate_program_path($git, "git");
vlog2 "directory: $directory
branch: $branch
";
set_timeout();
chdir($directory) or quit "CRITICAL", "Failed to chdir to directory '$directory'";
my @output = cmd("$git branch --color=never", 1);
foreach(@output){
if(/^\*\s+(.+)\s*$/){
$branch_checkout = $1;
last;
}
}
defined($branch_checkout) or quit "CRITICAL", "Failed to determine current branch checkout for directory '$directory'";
if($branch_checkout eq $branch){
quit "OK", "branch '$branch_checkout' currently checked out in directory '$directory'";
} else {
quit "CRITICAL", "branch '$branch_checkout' checked out, expecting '$branch' in directory '$directory'";
}