-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_git_commit_age.rb
executable file
·243 lines (201 loc) · 5.18 KB
/
check_git_commit_age.rb
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env ruby
# Coypright (C) 2018 Christian Mayer <christian@fox21.at>
# Check the age of the last Git commit.
require 'optparse'
require 'pathname'
require 'time'
STATES = ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN']
def write_file(path)
File.open(path, 'w') do |file|
file.write(Time.now.to_i.to_s)
end
end
def read_file(path)
File.read(path)
end
def convert_human_time(time_s)
time_s = time_s.strip.gsub(/ /, '')
res = /^(\d{1,11})(.?)$/.match(time_s)
n = res[1].to_i
h = {
'' => n,
's' => n,
'm' => n * 60,
'h' => n * 3600,
'd' => n * 24 * 3600,
'w' => n * 7 * 24 * 3600,
'M' => n * 30.41 * 24 * 3600,
'y' => n * 365 * 24 * 3600,
}
if not h.has_key?(res[2])
raise 'Invalid time string: %s' % [time_s]
end
h[res[2]].to_i
end
def convert_computer_time(s)
h = {
'y' => 365 * 24 * 3600,
'M' => 30.41 * 24 * 3600,
'w' => 7 * 24 * 3600,
'd' => 24 * 3600,
'h' => 3600,
'm' => 60,
}
prefix = ''
if s < 0
prefix = '-'
s = s.abs
end
x = Hash.new
h.each do |y, n|
m = 0
while s >= n && s >= 60 && m <= 1000
m += 1
if not x.has_key?(y)
x[y] = 0
end
x[y] += 1
s -= n
if s < 60
break
end
end
end
if s > 0 || x.keys.length == 0
x['s'] = s.to_i
end
(
'%s%s' % [
prefix,
x.map{ |k| '%d%s' % k.reverse }.join(''),
]
).strip
end
@options = {
:repo => nil,
:branch => nil,
:depth => nil,
:dst => nil,
:git_data => nil,
:git_pull_file => nil,
:warning_n => 3600,
:warning_s => '1h',
:critical_n => 7200,
:critical_s => '2h',
:pull_timeout => nil,
}
opts = OptionParser.new do |o|
o.banner = 'Usage: --repository <string> --branch <string> --depth <number> --destination <string> -w <time> -c <time>'
o.separator('')
o.on('-r', '--repository <string>', 'URL/path to the Git repository.') do |repo|
@options[:repo] = repo
end
o.on('-b', '--branch <string>', 'Switch to branch.') do |branch|
@options[:branch] = branch
end
o.on('-d', '--depth <number>', 'Git Depth') do |depth|
@options[:depth] = depth.to_i
end
o.on('-d', '--destination <string>', 'Path to local destination. For example /tmp/my-repo') do |dst|
@options[:dst] = Pathname.new(dst).expand_path
@options[:git_data] = Pathname.new('repo').expand_path(@options[:dst])
@options[:git_pull_file] = Pathname.new('git_pull').expand_path(@options[:dst])
end
o.on('-w', '--warning <time>', 'Warning (For example: 1h)') do |time|
@options[:warning_n] = convert_human_time(time)
@options[:warning_s] = time
end
o.on('-c', '--critical <time>', 'Critical (For example: 3h)') do |time|
@options[:critical_n] = convert_human_time(time)
@options[:critical_s] = time
end
o.on('-p', '--pull-timeout <time>', 'Time between pulls') do |time|
@options[:pull_timeout] = convert_human_time(time)
end
o.on_tail('-h', '--help', 'Show this message.') do
puts o
puts
exit 3
end
end
ARGV << '-h' if ARGV.count == 0
commands = opts.parse(ARGV)
if not @options[:dst].exist?
@options[:dst].mkpath
end
if not @options[:branch].nil?
# Do not use depth when switching to another branch.
@options[:depth] = nil
end
if not @options[:git_data].exist?
git_clone_cmds = [
'git clone --quiet',
]
if not @options[:depth].nil?
# Possible issue:
# When you use --depth for the very first clone you maybe cannot
# switch to another branch afterwards. You first have to delete the
# '--destination' folder and re-clone the complete repository with
# no depth set.
git_clone_cmds.push('--depth %d' % [@options[:depth]])
end
git_clone_cmds.push('"%s" "%s"' % [@options[:repo], @options[:git_data].to_s])
git_clone_cmd = git_clone_cmds.join(' ')
git_clone = system(git_clone_cmd)
if not git_clone
raise 'git clone failed'
end
write_file(@options[:git_pull_file])
end
diff_n = 0
diff_s = 'N/A'
next_pull = 0 # Pull always.
git_pull = false
Dir.chdir(@options[:dst]) do
Dir.chdir(@options[:git_data]) do
if not @options[:branch].nil?
git_checkout = system('git checkout --quiet -b %s origin/%s 2> /dev/null' % [
@options[:branch], @options[:branch],
])
if not git_checkout
git_checkout = system('git checkout --quiet %s 2> /dev/null' % [
@options[:branch],
])
if not git_checkout
raise 'git checkout failed'
end
end
end
if not @options[:pull_timeout].nil?
# Calc time for the next pull.
last_pull = read_file(@options[:git_pull_file]).to_i
next_pull = (Time.now - last_pull - @options[:pull_timeout]).to_i
end
if next_pull >= 0
git_pull = system('git pull --quiet')
if not git_pull
raise 'git pull failed'
end
write_file(@options[:git_pull_file])
end
# Get the Unix Timestamp.
uts = `git log -n 1 --pretty=format:%at`.to_i
# Calc diff
diff_n = Time.now.to_i - uts
diff_s = convert_computer_time(diff_n)
end
end
if diff_n >= @options[:critical_n]
state = 2
elsif diff_n >= @options[:warning_n]
state = 1
else
state = 0
end
state_name = STATES[state]
perf_data = [
state_name, diff_s, @options[:warning_s], @options[:critical_s], convert_computer_time(next_pull), git_pull ? 'Y' : 'N', # Normal Output
diff_n, @options[:warning_n], @options[:critical_n],
]
puts "%s: diff=%s w=%s c=%s n=%s p?=%s | diff=%ds;%d;%s" % perf_data
exit state