Commit 98ec801c authored by Marco Peterseil's avatar Marco Peterseil 🍺
Browse files

update to v0.6.0

parent 7919d34c
Pipeline #266704965 passed with stage
in 31 seconds
# changes by release
## 0.6.0
### features
* new check mode "ci-pipeline-status"
### other
* rename "ci-pipeline" to "ci-pipeline-duration"
* validate user input against possible modes
## 0.5.4
### other
* remove the optparser missing argument block
......
......@@ -48,14 +48,26 @@ naemon@gitlab:plugins$ ./check_gitlab.rb -m services
Critical - logrotate, mattermost is down
```
### CI Pipeline
### CI Pipeline duration
Checks duration of finished CI pipeline in seconds with perfdata. ([/projects/:id/pipelines](https://docs.gitlab.com/ee/api/pipelines.html#list-project-pipelines))
```shell
naemon@gitlab:plugins$ ./check_gitlab.rb -m ci-pipeline -s <gitlab_url> [-k] -t <access_token> -i <procject_id> -w 100 -c 200
naemon@gitlab:plugins$ ./check_gitlab.rb -m ci-pipeline-duration -s <gitlab_url> [-k] -t <access_token> -i <procject_id> -w 100 -c 200
Critical - Pipeline #265 took 265s | duration=265s;100;150
```
### CI Pipeline status
Checks status of latest CI pipeline. ([/projects/:id/pipelines](https://docs.gitlab.com/ee/api/pipelines.html#list-project-pipelines))
Options `-w` and `-c` allows regex.
Possible values for a ci pipeline status can be found [here](https://docs.gitlab.com/ee/api/pipelines.html#list-project-pipelines).
```shell
naemon@gitlab:plugins$ ./check_gitlab.rb -m ci-pipeline-status -s <gitlab_url> [-k] -t <access_token> -i <procject_id> -w pending -c 'failed|skipped'
OK - Status of pipeline #266693109: Success
```
### CI Runner status
Checks status of all registered CI runners. ([/runners/all](https://docs.gitlab.com/ee/api/runners.html#list-all-runners))
......@@ -66,7 +78,7 @@ Possible `--status` values are listed [here](https://docs.gitlab.com/ee/api/runn
Exclude runners via `--exclude` (regex)
```shell
naemon@gitlab:plugins$ ./check_gitlab.rb -m ci-runner -s <gitlab_url> [-k] -t <access_token> -w 2 -c 5 --status offline
naemon@gitlab:plugins$ ./check_gitlab.rb -m ci-runner-status -s <gitlab_url> [-k] -t <access_token> -w 2 -c 5 --status offline
Critical - Offline runners: srv-cir-04(10), workbot15(63), srv-cir-01(76), srv-cir-05(111), srv-cir-07(117), srv-cir-08(121), mac-01(131)
```
......
......@@ -28,7 +28,7 @@ require 'net/https'
require 'json'
require 'date'
version = 'v0.5.4'
version = 'v0.6.0'
# optparser
banner = <<~HEREDOC
......@@ -38,7 +38,8 @@ banner = <<~HEREDOC
health Check the Gitlab web endpoint for health
services Check if any service of 'gitlab-ctl status' is down
group-size Check size of group in MB
ci-pipeline Check duration of a CI pipeline
ci-pipeline-duration Check duration of a CI pipeline
ci-pipeline-status Check status of a CI pipeline
ci-runner-status Check status of CI runners
ci-runner-jobs-duration Check duration (in seconds) of running jobs of CI runners
license-expires Check remaining days when license expires - only warning status possible
......@@ -97,11 +98,13 @@ end.parse!
# check gitlab
class CheckGitlab
def initialize(options)
def initialize(options) # rubocop:disable Metrics/MethodLength
@options = options
init_arr
validate_check_modes
health_check
ci_pipeline_check
ci_pipeline_status
ci_pipeline_duration
ci_runner_jobs_duration
ci_runner_status
services_check
......@@ -144,6 +147,15 @@ class CheckGitlab
exit 3
end
def validate_check_modes
check_modes = %w[
health ci-pipeline-duration ci-pipeline-status
ci-runner-jobs-duration ci-runner-status services group-size
sidekiq-jobs license-expire license-overage
]
warn_msg('Mode not found. Check configuration.') unless check_modes.include?(@options[:mode])
end
# convert the bytes
def convert_to_mb(data:)
@used_size = data.to_i / 1024 / 1024
......@@ -163,8 +175,8 @@ class CheckGitlab
@message = msg
end
# helper for threshold checking
def check_thresholds(data:, type: 'non-array')
### helpers for threshold checking
def check_thresholds_int(data:, type: 'non-array')
if data > @options[:critical].to_i
@critical << @message
elsif data > @options[:warning].to_i
......@@ -176,15 +188,27 @@ class CheckGitlab
build_final_output unless type != 'non-array'
end
def check_thresholds_string(data:)
case data
when /#{@options[:critical]}/
@critical << @message
when /#{@options[:warning]}/
@warning << @message
else
@okays << @message
end
build_final_output(perf_enabled: false)
end
# mix everything together for exit
def build_final_output(pretext: '')
perf_output = " | #{@perfdata.join(' ')}"
def build_final_output(pretext: '', perf_enabled: true)
perf_output = " | #{@perfdata.join(' ')}" unless perf_enabled == false
if @critical.any?
crit_msg(pretext + @critical.join(', ') + perf_output)
crit_msg(pretext + @critical.join(', ') + perf_output.to_s)
elsif @warning.any?
warn_msg(pretext + @warning.join(', ') + perf_output)
warn_msg(pretext + @warning.join(', ') + perf_output.to_s)
else
ok_msg(pretext + @okays.join(', ') + perf_output)
ok_msg(pretext + @okays.join(', ') + perf_output.to_s)
end
end
......@@ -235,16 +259,25 @@ class CheckGitlab
end
end
###--- CI-PIPELINE STATUS CHECK ---###
def ci_pipeline_status
return unless @options[:mode] == 'ci-pipeline-status'
http_connect(path: "api/v4/projects/#{@options[:id]}/pipelines")
ci_pipeline_data = JSON.parse(@response.body).first
build_output(msg: "Status of pipeline ##{ci_pipeline_data['id']}: #{ci_pipeline_data['status'].capitalize}")
check_thresholds_string(data: ci_pipeline_data['status'])
end
###--- CI-PIPELINE DURATION CHECK ---###
def ci_pipeline_check
return unless @options[:mode] == 'ci-pipeline'
def ci_pipeline_duration
return unless @options[:mode] == 'ci-pipeline-duration'
http_connect(path: "api/v4/projects/#{@options[:id]}/pipelines?scope=finished")
# get latest pipeline
http_connect(path: "api/v4/projects/#{@options[:id]}/pipelines/#{JSON.parse(@response.body).first['id']}")
ci_pipeline_data = JSON.parse(@response.body)
build_output(msg: "Pipeline ##{ci_pipeline_data['id']} took #{ci_pipeline_data['duration']}s")
build_perfdata(perfdata: "duration=#{ci_pipeline_data['duration']}s")
check_thresholds(data: ci_pipeline_data['duration'])
check_thresholds_int(data: ci_pipeline_data['duration'])
end
###--- CI-RUNNER ---###
......@@ -270,7 +303,7 @@ class CheckGitlab
else
build_output(msg: "#{jobs['name']} is running for #{jobs['duration'].round}s")
build_perfdata(perfdata: "duration=#{jobs['duration'].round}s")
check_thresholds(data: jobs['duration'])
check_thresholds_int(data: jobs['duration'])
end
end
......@@ -282,7 +315,7 @@ class CheckGitlab
http_connect(path: "api/v4/runners/#{item['id']}")
@runners = JSON.parse(@response.body)
build_output(msg: "#{@runners['description']}(#{@runners['id']})")
check_thresholds(type: 'array', data: @all_runners.count)
check_thresholds_int(type: 'array', data: @all_runners.count)
end
build_perfdata(perfdata: "#{@options[:status]}=#{@all_runners.count}")
build_final_output(pretext: "#{@options[:status].capitalize} runners: ")
......@@ -329,7 +362,7 @@ class CheckGitlab
@all_groups.each do |item|
convert_to_mb(data: item['statistics']['storage_size'])
build_output(msg: "#{item['name']}: used space #{@used_size}MB")
check_thresholds(type: 'array', data: @used_size)
check_thresholds_int(type: 'array', data: @used_size)
build_perfdata(perfdata: "#{item['name']}=#{@used_size}MB")
end
build_final_output
......@@ -354,7 +387,7 @@ class CheckGitlab
data = JSON.parse(@response.body)
build_output(msg: "Active users: #{data['active_users']}, Overage: #{data['overage']}")
build_perfdata(perfdata: "active_users=#{data['active_users']} overage=#{data['overage']}")
check_thresholds(data: data['overage'])
check_thresholds_int(data: data['overage'])
end
###--- SIDEKIQ ---###
......@@ -364,7 +397,7 @@ class CheckGitlab
data = JSON.parse(@response.body)['jobs']
build_output(msg: "Sidekiq jobs - #{@options[:name].capitalize}: #{data[@options[:name]]}")
build_perfdata(perfdata: "#{@options[:name]}=#{data[@options[:name]]}")
check_thresholds(data: data[@options[:name]])
check_thresholds_int(data: data[@options[:name]])
end
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment