Skip to content

Commit

Permalink
Merge pull request #32 from pavelkim/bugfix/issue-31/failed-domain-no…
Browse files Browse the repository at this point in the history
…t-in-metrics

gh-31 [Change] Error/OK status tracking reimplemented.
  • Loading branch information
pavelkim authored Aug 24, 2024
2 parents 6b677b7 + f5de8bc commit 8aeae1b
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 41 deletions.
17 changes: 6 additions & 11 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,11 @@ A clear and concise description of what you expected to happen.
**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Environment:**
- OS: [e.g. CentOS 7]
- docker image: [e.g. check_certificates:1.8.0]
- openssl: [e.g. 1.1.1k or N/A]
- bash: [e.g. 4.4.20 or N/A]

**Additional context**
Add any other context about the problem here.
6 changes: 3 additions & 3 deletions .github/workflows/pr_to_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ jobs:
steps:

- name: Check out code
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0


- name: Use latest released action
id: validate_new_version
uses: reinvented-stuff/validate-version-action@1.1.3
uses: reinvented-stuff/validate-version-action@1.2.0
with:
version_filename: ".version"
github_token: "${{ secrets.GITHUB_TOKEN }}"
Expand All @@ -41,7 +41,7 @@ jobs:
steps:

- name: Check out code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Test run
run: |
Expand Down
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.1
1.9.0
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ imaginary-domain-9000.com
Output fields:
```
Hostname Valid Not Before Valid Not After Expires in N Days
Hostname Valid Not Before Valid Not After Expires in N Days Outcome
```
Full output (default) example:
```
imaginary-domain-9000.com error error -1
google.com 2020-06-30 20:43:12 2020-09-22 20:43:12 66
example.com 2018-11-28 00:00:00 2020-12-02 12:00:00 136
mail.com 2018-01-15 00:00:00 2021-01-14 12:00:00 179
imaginary-domain-9000.com error error error error error error
google.com 2020-06-30 20:43:12 2020-09-22 20:43:12 66 ok
example.com 2018-11-28 00:00:00 2020-12-02 12:00:00 136 ok
mail.com 2018-01-15 00:00:00 2021-01-14 12:00:00 179 ok
```
Domain names only output (with parameters `-n -l -A 90`) example:
Expand All @@ -158,9 +158,9 @@ PROMETHEUS_EXPORT_FILENAME="/path/to/htdocs/metrics"
```prometheus
# HELP check_certificates_expiration Days until HTTPs SSL certificate expires (skipped on error)
# TYPE check_certificates_expiration counter
check_certificates_expiration{domain="example.com"} 20
check_certificates_expiration{domain="example.de"} 193
check_certificates_expiration{domain="imaginary-domain-9000.com"} -1
check_certificates_expiration{domain="example.com",outcome="ok"} 20
check_certificates_expiration{domain="example.de",outcome="ok"} 193
check_certificates_expiration{domain="imaginary-domain-9000.com",outcome="error"} -1
```
## nginx configuration example
Expand Down
33 changes: 18 additions & 15 deletions check_certificates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,21 @@ generate_prometheus_metrics() {
local full_result_item
local full_result_item_parts
local metrics_item
local metrics_labels

[[ ! -z "$*" ]] && full_result=( "$@" ) || error "Formatted result list not set!"

info "Exporting Prometheus metrics into file '${PROMETHEUS_EXPORT_FILENAME}'"

info "Writing Prometheus metrics header (overwriting)"
echo "# HELP check_certificates_expiration Days until HTTPs SSL certificate expires (skipped on error)" > "${PROMETHEUS_EXPORT_FILENAME}"
echo "# HELP check_certificates_expiration Days until HTTPs SSL certificate expires" > "${PROMETHEUS_EXPORT_FILENAME}"
echo "# TYPE check_certificates_expiration gauge" >> "${PROMETHEUS_EXPORT_FILENAME}"

for full_result_item in "${full_result[@]}"; do
full_result_item_parts=( ${full_result_item} )
# shellcheck disable=SC2004
metrics_item="${metrics_name}{domain=\"${full_result_item_parts[0]}\"} $(( (${full_result_item_parts[2]} - ${TODAY_TIMESTAMP}) / 86400 ))"
metrics_labels="domain=\"${full_result_item_parts[0]}\",outcome=\"${full_result_item_parts[3]}\""
metrics_item="${metrics_name}{${metrics_labels}} $(( (${full_result_item_parts[2]} - ${TODAY_TIMESTAMP}) / 86400 ))"
info "Writing metrics item '${metrics_item}'"
echo "${metrics_item}" >> "${PROMETHEUS_EXPORT_FILENAME}"
done
Expand Down Expand Up @@ -230,7 +232,7 @@ check_https_certificate_dates() {
if [[ "${RC}" != "0" ]]; then

warning "Can't process openssl output for ${remote_hostname}"
final_result="${remote_hostname} error error"
final_result="${remote_hostname} ${TODAY_TIMESTAMP} ${TODAY_TIMESTAMP} error"

else

Expand All @@ -242,7 +244,7 @@ check_https_certificate_dates() {
info "${remote_hostname} Not before ${dates[0]}"
info "${remote_hostname} Not after ${dates[1]}"

final_result="${remote_hostname} $(date_to_epoch "${dates[0]}") $(date_to_epoch "${dates[1]}")"
final_result="${remote_hostname} $(date_to_epoch "${dates[0]}") $(date_to_epoch "${dates[1]}") ok"
fi

echo "${final_result}"
Expand Down Expand Up @@ -279,7 +281,6 @@ main() {
local CLI_VERBOSE

local full_result=( )
local error_result=( )
local formatted_result=( )
local sorted_result=( )
local input_filename
Expand Down Expand Up @@ -368,8 +369,8 @@ main() {
rc="$?"

if [[ "${rc}" != "0" ]]; then
warning "Skipping '${remote_hostname}'"
error_result+=( "${remote_hostname}" )
warning "Labeling '${remote_hostname}' as failed to get validated"
full_result+=( "${current_result}" )
else
info "Adding item into full_result: '${current_result}'"
full_result+=( "${current_result}" )
Expand All @@ -379,18 +380,12 @@ main() {

done < "${input_filename}"

if [[ "${#full_result[@]}" -le "0" ]]; then
if [[ "${#full_result[@]}" -eq "0" ]]; then
warning "Couldn't process anything from '${input_filename}'"
else
info "Processed '${#full_result[@]}' items from '${input_filename}'"
fi

if [[ "${#error_result[@]}" -gt "0" ]]; then
for error_item in "${error_result[@]}"; do
formatted_result+=( "${error_item} error error -1" )
done
fi

if [[ "${CLI_GENERATE_METRICS}" == "1" ]]; then
info "Generating Prometheus metrics"
generate_prometheus_metrics "${full_result[@]}"
Expand All @@ -408,7 +403,15 @@ main() {
fi
fi

formatted_result_item="${result_item_parts[0]} $(epoch_to_date "${result_item_parts[1]}" "+%F %T") $(epoch_to_date "${result_item_parts[2]}" "+%F %T") $(( (result_item_parts[2] - TODAY_TIMESTAMP) / 86400 ))"
if [[ "${result_item_parts[3]}" == "ok" ]]; then
formatted_result_item="${result_item_parts[0]} $(epoch_to_date "${result_item_parts[1]}" "+%F %T") $(epoch_to_date "${result_item_parts[2]}" "+%F %T") $(( (result_item_parts[2] - TODAY_TIMESTAMP) / 86400 )) ok"

elif [[ "${result_item_parts[3]}" == "error" ]]; then
formatted_result_item="${result_item_parts[0]} error error error error error error"
else
warning "Couldn't identify status for ${result_item_parts[0]}: '${result_item_parts[3]}'"
fi

info "Rendering a formatted result item: '${formatted_result_item}'"
formatted_result+=( "${formatted_result_item}" )
done
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:
- "./docker/usr/share/nginx/htdocs:/usr/share/nginx/htdocs"

check_certificates:
image: ghcr.io/pavelkim/check_certificates/check_certificates:1.7.2dev
image: ghcr.io/pavelkim/check_certificates/check_certificates:1.9.0
container_name: check_certificates-daemon
environment:
CHECK_INTERVAL: 7200
Expand Down
2 changes: 1 addition & 1 deletion test/expected_result.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
imaginary-domain-9000.com error error -1
imaginary-domain-9000.com error error error error error error
2 changes: 1 addition & 1 deletion wrapper_loop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Community support:
# https://github.com/pavelkim/check_certificates/issues
#
# Copyright © 2022, Pavel Kim
# Copyright 2022, Pavel Kim - All Rights Reserved
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down

0 comments on commit 8aeae1b

Please sign in to comment.