Skip to content

added cronjob check #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions check_rancher2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ Check Types:
\tproject -> Checks the current status of all projects or of a specific project (defined with -p projectid)
\tworkload -> Checks the current status of all or a specific (-w workloadname) workload within a project (-p projectid must be set!)
\tpod -> Checks the current status of all or a specific (-o podname -n namespace) pod within a project (-p projectid must be set!)
\tcron -> Checks the current status of a single (-w workloadname) cronjob within a project (-p projectid and -w workloadname must be set!)
"
exit ${STATE_UNKNOWN}
}
Expand Down Expand Up @@ -1171,6 +1172,79 @@ else
fi
;;

# --- cronjob status check (requires project, namespace and workload name)--- #

cron)
if [ -z $projectname ] || [ -z $workloadname ] || [ -z $namespacename ]; then
echo -e "CHECK_RANCHER2 UNKNOWN - To check a cronjob you must define the project (-p), workloadname (-w) and the namespace (-n)."
exit ${STATE_UNKNOWN}
fi

if [[ -n $namespacename && $namespacename != "" ]]; then
nsappend="&namespaceId=$namespacename"
nsoutputappend="in namespace $namespacename "
fi

api_out_single_cronjob=$(curl -s ${selfsigned} -u "${apiuser}:${apipass}" "${proto}://${apihost}/v3/project/${projectname}/workloads/cronjob:${namespacename}:${workloadname}")

# Check if cluster is available
if [[ -n $(echo "$api_out_single_cronjob" | grep -i "ClusterUnavailable") ]]; then
clustername=$(echo ${projectname} | awk -F':' '{print $1}')
echo "CHECK_RANCHER2 CRITICAL - Cluster $clustername not found. Hint: Use '-t info' to identify cluster and project names."
exit ${STATE_CRITICAL}
fi

# Check if that given project name exists
if [[ -z $(echo "$api_out_single_cronjob" | grep -i "containers") ]]; then
echo "CHECK_RANCHER2 CRITICAL - Cronjob $workloadname ${nsoutputappend}not found."
exit ${STATE_CRITICAL}
fi

# Path to cron schedule converter
cronconverter=./convert_cron_schedule.py

# Check healthstatus of cronjob
healthstatus=$(echo "$api_out_single_cronjob" | jq -r '.state')
suspended=$(echo "$api_out_single_cronjob" | jq -r '.cronJobConfig.suspend')

# Check if cronjob is active and runs in defined schedule
if test -f "$cronconverter"; then

if [[ ${healthstatus} = active ]]; then
cronschedule=$(echo "$api_out_single_cronjob" | jq -r '.cronJobConfig.schedule')
crondiff=$(python3 $cronconverter "$cronschedule" difftimestamp)
currenttime=$(date +%s%N | cut -b1-13)
lastruntime=$(echo "$api_out_single_cronjob" | jq -r '.cronJobStatus.lastScheduleTimeTS')
currentdiff=$((currenttime-lastruntime))

if [[ ${suspended} = true ]]; then
echo "CHECK_RANCHER2 WARNING - Cronjob $workloadname ${nsoutputappend}is suspended.|'workload_active'=0;;;; 'workload_error'=0;;;; 'workload_warning'=1;;;;"
exit ${STATE_WARNING}

elif [[ $currentdiff -gt $crondiff ]]; then
echo "CHECK_RANCHER2 CRITICAL - Cronjob $workloadname ${nsoutputappend}is not running in schedule time."
exit ${STATE_CRITICAL}
else
echo "CHECK_RANCHER2 OK - Cronjob $workloadname ${nsoutputappend}is running properly."
exit ${STATE_OK}
fi

elif [[ ${healthstatus} = updating ]]; then
echo "CHECK_RANCHER2 WARNING - Cronjob $workloadname ${nsoutputappend}is ${healthstatus}|'workload_active'=0;;;; 'workload_error'=0;;;; 'workload_warning'=1;;;;"
exit ${STATE_WARNING}

else
echo "CHECK_RANCHER2 CRITICAL - Cronjob $workloadname ${nsoutputappend}is ${healthstatus}|'workload_active'=0;;;; 'workload_error'=1;;;; 'workload_warning'=0;;;;"
exit ${STATE_CRITICAL}
fi

else
echo "CHECK_RANCHER2 UNKNOWN - Resource not found: convert_cron_schedule.py"
exit ${STATE_UNKNOWN}
fi
;;


esac
echo "UNKNOWN: should never reach this part"
exit ${STATE_UNKNOWN}
28 changes: 28 additions & 0 deletions convert_cron_schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import croniter
import datetime
import sys

# Variables
now = datetime.datetime.now()
sched = sys.argv[1]
argument = sys.argv[2]
cron = croniter.croniter(sched, now)

# Dates
nextdate = cron.get_next(datetime.datetime)
nextdatets_int = int(round(nextdate.timestamp() * 1000))
prevdate = cron.get_prev(datetime.datetime)
prevdatets_int = int(round(prevdate.timestamp() * 1000))

schedulediff = nextdatets_int - prevdatets_int

# Get next cron run
if argument == "next_run":
print(nextdatets_int)

# Get difference between cron runs
elif argument == "difftimestamp":
print(schedulediff)

else:
print("Unknown arguments.")