Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F56330197
ccache-metrics.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
ccache-metrics.py
View Options
#!/usr/bin/env python3
# -------------------------------------------------------------
# OpenMetrics statistics for ccache
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Description: Convert ccache -s output into OpenMetrics format
# License: BSD-2-Clause
# Source file: roles/devserver/userland-software/files/ccache-metrics.py
# -------------------------------------------------------------
#
# <auto-generated>
# This file is managed by our rOPS SaltStack repository.
#
# Changes to this file may cause incorrect behavior
# and will be lost if the state is redeployed.
# </auto-generated>
from
datetime
import
datetime
import
re
import
subprocess
import
time
CCACHE_DATE_FORMAT
=
"%a %b
%d
%H:%M:%S %Y"
PREFIX
=
"ccache"
# -------------------------------------------------------------
# OpenMetrics
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def
format_metric
(
key
,
value
,
prefix
):
metric_key
=
f
"{prefix}_{key}"
if
key
.
endswith
(
"_directory"
)
or
key
.
endswith
(
"_config"
):
if
not
key
.
endswith
(
"_info"
):
metric_key
+=
"_info"
metric_key
+=
"{path=
\"
"
+
value
+
"
\"
}"
type
=
"info"
value
=
1
elif
key
.
endswith
(
"_rate"
)
or
key
.
endswith
(
"_size"
)
or
key
==
"files_in_cache"
:
type
=
"gauge"
else
:
if
not
key
.
endswith
(
"_total"
):
metric_key
+=
"_total"
type
=
"counter"
return
f
"# TYPE {prefix}_{key} {type}
\n
{metric_key} {value}"
def
format_openmetrics
(
stats
,
prefix
):
return
[
format_metric
(
key
,
value
,
prefix
)
for
key
,
value
in
stats
.
items
()]
# -------------------------------------------------------------
# ccache
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def
query_ccache_stats
():
raw_stats
=
call_ccache_stats
()
.
strip
()
.
split
(
"
\n
"
)
stats
=
[
parse_ccache_stat_line
(
line
)
for
line
in
raw_stats
]
return
dict
(
stats
)
def
call_ccache_stats
():
process
=
subprocess
.
run
([
"ccache"
,
"-s"
],
capture_output
=
True
)
return
process
.
stdout
.
decode
()
def
format_key
(
key
):
replace_map
=
{
" "
:
"_"
,
"("
:
""
,
")"
:
""
,
"/"
:
"_"
}
for
k
,
v
in
replace_map
.
items
():
key
=
key
.
replace
(
k
,
v
)
return
key
def
parse_date
(
value
):
"""Parse a ccache date in UTC into a UNIX timestamp."""
dt
=
datetime
.
strptime
(
value
,
CCACHE_DATE_FORMAT
)
return
int
(
time
.
mktime
(
dt
.
timetuple
()))
def
parse_size
(
value
):
"""Parse a ccache disk size (kB, MB, GB) into bytes."""
tokens
=
value
.
split
(
" "
)
size
=
tokens
[
0
]
unit
=
tokens
[
1
]
if
unit
==
"kB"
:
return
float
(
size
)
*
1024
elif
unit
==
"MB"
:
return
float
(
size
)
*
1024
*
1024
elif
unit
==
"GB"
:
return
float
(
size
)
*
1024
*
1024
*
1024
raise
ValueError
(
value
)
def
parse_value
(
key
,
value
):
# Date
if
key
==
"stats_updated"
:
return
parse_date
(
value
)
# Percent
if
value
.
endswith
(
"%"
):
token
=
value
.
split
(
" "
)[
0
]
return
float
(
token
)
/
100
# Disk size
if
key
.
endswith
(
"_size"
):
return
parse_size
(
value
)
return
value
def
parse_ccache_stat_line
(
line
):
tokens
=
re
.
sub
(
r" {3,}"
,
"
\t
"
,
line
)
.
split
(
"
\t
"
)
key
=
format_key
(
tokens
[
0
])
value
=
parse_value
(
key
,
tokens
[
-
1
])
return
key
,
value
# -------------------------------------------------------------
# Application entry point
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def
run
():
ccache_stats
=
query_ccache_stats
()
ccache_metrics
=
format_openmetrics
(
ccache_stats
,
PREFIX
)
for
metric
in
ccache_metrics
:
print
(
metric
)
print
(
"# EOF"
)
if
__name__
==
"__main__"
:
run
()
File Metadata
Details
Attached
Mime Type
text/x-python
Expires
Sat, Nov 7, 02:50 (1 d, 23 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3958863
Default Alt Text
ccache-metrics.py (3 KB)
Attached To
Mode
rOPS Nasqueron Operations
Attached
Detach File
Event Timeline
Log In to Comment