Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Bram Daams
sch
Commits
789b3c64
Commit
789b3c64
authored
Jan 20, 2020
by
Bram Daams
Browse files
sake_cased_methods and more fixes to please pylint
parent
c9154d0e
Pipeline
#36628
failed with stage
in 1 minute and 27 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
789b3c64
...
@@ -12,20 +12,24 @@ $ pip install .
...
@@ -12,20 +12,24 @@ $ pip install .
## Test
## Test
For now, create a file
`test.py`
For now, create a file
`test.py`
```
python
```
python
from
hc
import
hcCred
,
Healthchecks
from
crontabs
import
CronTabs
from
hc
import
HealthcheckCredentials
,
Healthchecks
cred
=
HealthcheckCredentials
(
api_url
=
'https://hc.example.com/api/v1/'
,
api_key
=
'mysecretapikey'
)
cred
=
hcCred
(
'https://hc.example.com/api/v1/'
,
'mysecretapikey'
)
h
=
Healthchecks
(
cred
)
h
=
Healthchecks
(
cred
)
h
.
PrintStatus
()
h
.
PrintStatus
()
# scan jobs that want to use SCH
jobs
=
CronTabs
().
all
.
find_command
(
'JOB_ID'
)
jobs
=
CronTabs
().
all
.
find_command
(
'JOB_ID'
)
for
job
in
jobs
:
for
job
in
jobs
:
check
=
h
.
F
ind
C
heck
(
job
)
check
=
h
.
f
ind
_c
heck
(
job
)
if
check
:
if
check
:
h
.
U
pdate
C
heck
(
check
,
job
)
h
.
u
pdate
_c
heck
(
check
,
job
)
else
:
else
:
h
.
N
ew
C
heck
(
job
)
h
.
n
ew
_c
heck
(
job
)
```
```
And run it within the virtual environment
And run it within the virtual environment
...
...
hc.py
View file @
789b3c64
...
@@ -2,29 +2,22 @@
...
@@ -2,29 +2,22 @@
module for interfacing a healthchecks.io compatible service
module for interfacing a healthchecks.io compatible service
"""
"""
import
sys
import
sys
import
click
import
arrow
import
hashlib
import
hashlib
import
platform
import
platform
import
re
import
re
import
collections
import
arrow
import
click
import
requests
import
requests
import
tzlocal
import
tzlocal
HealthcheckCredentials
=
collections
.
namedtuple
(
'HealthcheckCredentials'
,
'api_url api_key'
)
class
hcCred
:
class
Healthchecks
:
"""
"""
Object to store the healthchecks api url and key
Interfaces with e healthckecks.io compatible API to register
cron jobs found on the system.
"""
"""
def
__init__
(
self
,
url
,
api_key
):
self
.
url
=
url
self
.
api_key
=
api_key
def
__repr__
(
self
):
return
"""healthchecks api access credentials
to {url}"""
.
format
(
url
=
self
.
url
)
class
Healthchecks
:
def
__init__
(
self
,
cred
):
def
__init__
(
self
,
cred
):
self
.
cred
=
cred
self
.
cred
=
cred
self
.
auth_headers
=
{
'X-Api-Key'
:
self
.
cred
.
api_key
}
self
.
auth_headers
=
{
'X-Api-Key'
:
self
.
cred
.
api_key
}
...
@@ -32,7 +25,7 @@ class Healthchecks:
...
@@ -32,7 +25,7 @@ class Healthchecks:
def
get_checks
(
self
):
def
get_checks
(
self
):
"""Returns a list of checks from the HC API"""
"""Returns a list of checks from the HC API"""
url
=
"{}checks/"
.
format
(
self
.
cred
.
url
)
url
=
"{}checks/"
.
format
(
self
.
cred
.
api_
url
)
try
:
try
:
response
=
requests
.
get
(
url
,
headers
=
self
.
auth_headers
)
response
=
requests
.
get
(
url
,
headers
=
self
.
auth_headers
)
...
@@ -45,11 +38,11 @@ class Healthchecks:
...
@@ -45,11 +38,11 @@ class Healthchecks:
raise
Exception
(
'fetching cron checks failed'
)
raise
Exception
(
'fetching cron checks failed'
)
def
F
ind
C
heck
(
self
,
job
):
def
f
ind
_c
heck
(
self
,
job
):
"""
"""
Find a check in Healthchecks for the host and given job
Find a check in Healthchecks for the host and given job
"""
"""
job_id
=
self
.
G
et
JobI
d
(
job
)
job_id
=
self
.
g
et
_job_i
d
(
job
)
tag_for_job
=
'job_id={job_id}'
.
format
(
job_id
=
job_id
)
tag_for_job
=
'job_id={job_id}'
.
format
(
job_id
=
job_id
)
tag_for_host
=
'host={hostname}'
.
format
(
hostname
=
platform
.
node
())
tag_for_host
=
'host={hostname}'
.
format
(
hostname
=
platform
.
node
())
...
@@ -69,18 +62,20 @@ class Healthchecks:
...
@@ -69,18 +62,20 @@ class Healthchecks:
return
None
return
None
def
GetJobTags
(
self
,
job
):
@
staticmethod
def
get_job_tags
(
job
):
"""
"""
Returns the tags specified in the environment variable
Returns the tags specified in the environment variable
JOB_TAGS in the cron job
JOB_TAGS in the cron job
"""
"""
regex
=
r
'.*JOB_TAGS=([\w,]*)'
regex
=
r
'.*JOB_TAGS=([\w,]*)'
m
=
re
.
match
(
regex
,
job
.
command
)
m
atch
=
re
.
match
(
regex
,
job
.
command
)
if
m
:
if
m
atch
:
return
m
.
group
(
1
).
replace
(
','
,
' '
)
return
m
atch
.
group
(
1
).
replace
(
','
,
' '
)
return
""
return
""
def
GetJobId
(
self
,
job
):
@
staticmethod
def
get_job_id
(
job
):
"""
"""
Returns the value of environment variable JOB_ID if specified
Returns the value of environment variable JOB_ID if specified
in the cron job
in the cron job
...
@@ -92,7 +87,8 @@ class Healthchecks:
...
@@ -92,7 +87,8 @@ class Healthchecks:
return
None
return
None
def
GenerateJobHash
(
self
,
job
):
@
staticmethod
def
generate_job_hash
(
job
):
"""Returns the unique hash for given cron job"""
"""Returns the unique hash for given cron job"""
md5
=
hashlib
.
md5
()
md5
=
hashlib
.
md5
()
md5
.
update
(
platform
.
node
().
encode
(
'utf-8'
))
md5
.
update
(
platform
.
node
().
encode
(
'utf-8'
))
...
@@ -100,7 +96,16 @@ class Healthchecks:
...
@@ -100,7 +96,16 @@ class Healthchecks:
md5
.
update
(
job
.
command
.
encode
(
'utf-8'
))
md5
.
update
(
job
.
command
.
encode
(
'utf-8'
))
return
md5
.
hexdigest
()
return
md5
.
hexdigest
()
def
GetCheckHash
(
self
,
check
):
@
staticmethod
def
get_check_hash
(
check
):
"""
returns the hash stored in a tag of a healthchecks check
the tags lookes like:
hash=fdec0d88e53cc57ef666c8ec548c88bb
returns None if the tag is not found
"""
regex
=
r
"hash=(\w*)"
regex
=
r
"hash=(\w*)"
hash_search
=
re
.
search
(
regex
,
check
[
'tags'
])
hash_search
=
re
.
search
(
regex
,
check
[
'tags'
])
...
@@ -109,9 +114,12 @@ class Healthchecks:
...
@@ -109,9 +114,12 @@ class Healthchecks:
return
None
return
None
def
UpdateCheck
(
self
,
check
,
job
):
def
update_check
(
self
,
check
,
job
):
job_hash
=
self
.
GenerateJobHash
(
job
)
"""
check_hash
=
self
.
GetCheckHash
(
check
)
update check metadata for given cron job
"""
job_hash
=
self
.
generate_job_hash
(
job
)
check_hash
=
self
.
get_check_hash
(
check
)
if
check_hash
:
if
check_hash
:
if
job_hash
==
check_hash
:
if
job_hash
==
check_hash
:
...
@@ -131,11 +139,11 @@ class Healthchecks:
...
@@ -131,11 +139,11 @@ class Healthchecks:
'tz'
:
tzlocal
.
get_localzone
().
zone
,
'tz'
:
tzlocal
.
get_localzone
().
zone
,
'tags'
:
'sch host={host} job_id={job_id} '
'tags'
:
'sch host={host} job_id={job_id} '
'hash={hash} {tags}'
.
format
(
'hash={hash} {tags}'
.
format
(
host
=
platform
.
node
(),
host
=
platform
.
node
(),
job_id
=
self
.
G
et
JobI
d
(
job
),
job_id
=
self
.
g
et
_job_i
d
(
job
),
hash
=
job_hash
,
hash
=
job_hash
,
tags
=
self
.
G
et
JobT
ags
(
job
)
tags
=
self
.
g
et
_job_t
ags
(
job
)
)
)
}
}
# post the data
# post the data
...
@@ -154,8 +162,11 @@ class Healthchecks:
...
@@ -154,8 +162,11 @@ class Healthchecks:
return
True
return
True
def
NewCheck
(
self
,
job
):
def
new_check
(
self
,
job
):
job_hash
=
self
.
GenerateJobHash
(
job
)
"""
creates a new check for given job
"""
job_hash
=
self
.
generate_job_hash
(
job
)
# gather all the jobs' metadata
# gather all the jobs' metadata
data
=
{
data
=
{
...
@@ -167,17 +178,17 @@ class Healthchecks:
...
@@ -167,17 +178,17 @@ class Healthchecks:
'tz'
:
tzlocal
.
get_localzone
().
zone
,
'tz'
:
tzlocal
.
get_localzone
().
zone
,
'tags'
:
'sch host={host} job_id={job_id} '
'tags'
:
'sch host={host} job_id={job_id} '
'hash={hash} {tags}'
.
format
(
'hash={hash} {tags}'
.
format
(
host
=
platform
.
node
(),
host
=
platform
.
node
(),
job_id
=
self
.
G
et
JobI
d
(
job
),
job_id
=
self
.
g
et
_job_i
d
(
job
),
hash
=
job_hash
,
hash
=
job_hash
,
tags
=
self
.
G
et
JobT
ags
(
job
)
tags
=
self
.
g
et
_job_t
ags
(
job
)
)
)
}
}
# post the data
# post the data
try
:
try
:
response
=
requests
.
post
(
response
=
requests
.
post
(
url
=
'{}/checks/'
.
format
(
self
.
cred
.
url
),
url
=
'{}/checks/'
.
format
(
self
.
cred
.
api_
url
),
headers
=
self
.
auth_headers
,
headers
=
self
.
auth_headers
,
json
=
data
json
=
data
)
)
...
@@ -191,7 +202,7 @@ class Healthchecks:
...
@@ -191,7 +202,7 @@ class Healthchecks:
print
(
'check created'
)
print
(
'check created'
)
return
True
return
True
def
P
rint
S
tatus
(
self
,
status_filter
=
""
):
def
p
rint
_s
tatus
(
self
,
status_filter
=
""
):
"""Show status of monitored cron jobs"""
"""Show status of monitored cron jobs"""
click
.
secho
(
"{status:<6} {last_ping:<15} {name:<40}"
.
format
(
click
.
secho
(
"{status:<6} {last_ping:<15} {name:<40}"
.
format
(
status
=
"Status"
,
status
=
"Status"
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment