3 # - global, builtin : ALLCAPS
4 # - global, public : Camel_Snake_Man_Bear_Pig
5 # - global, private : _snake_case_prefixed_underscore
8 # - global, public : snake_case
11 GC_Interval = GC_Interval ? GC_Interval
: 3600 # seconds
13 _total_to_diff
["khatus_sensor_net_addr_io", "bytes_read" ] =
1
14 _total_to_diff
["khatus_sensor_net_addr_io", "bytes_written" ] =
1
15 _total_to_diff
["khatus_sensor_disk_io" , "sectors_read" ] =
1
16 _total_to_diff
["khatus_sensor_disk_io" , "sectors_written"] =
1
20 # (x * bytes_per_sector) / bytes_per_mb = x * scaling_factor
21 # ==> scaling_factor = bytes_per_sector / bytes_per_mb
22 _bytes_per_sector =
512
23 _bytes_per_mb =
1024 * 1024
24 _scale
["khatus_sensor_disk_io", "sectors_written"] = _bytes_per_sector
/ _bytes_per_mb
25 _scale
["khatus_sensor_disk_io", "sectors_read" ] = _bytes_per_sector
/ _bytes_per_mb
28 # x / bytes_per_mb = x * scaling_factor
29 # ==> scaling_factor = 1 / bytes_per_mb
30 _scale
["khatus_sensor_net_addr_io", "bytes_written"] =
1 / _bytes_per_mb
31 _scale
["khatus_sensor_net_addr_io", "bytes_read" ] =
1 / _bytes_per_mb
34 function cache_update
( src
, key
, val
, len_line
, len_head
, len_val
, time
) {
37 # Not just using $4 for val - because an unstructured value (like name of a
38 # song) might contain a character identical to FS
40 len_head =
length($
1 FS $
2 FS $
3 FS)
41 len_val = len_line
- len_head
42 val =
substr($
0, len_head
+ 1, len_val
)
43 val = cache_maybe_total_to_diff
(src
, key
, val
)
44 val = cache_maybe_scale
(src
, key
, val
)
45 _cache
[src
, key
] = val
46 time = cache_get_time
()
47 _cache_mtime
[src
, key
] = time
48 if (time % GC_Interval ==
0) {
53 function cache_get
(result
, src
, key
, ttl
, time
, age
, is_expired
) {
54 time = cache_get_time
()
55 _cache_atime
[src
, key
] = time
56 age = time
- _cache_mtime
[src
, key
]
57 result
["is_expired"] = ttl
&& age
> ttl
# ttl = 0 => forever
58 result
["value"] = _cache
[src
, key
]
61 function cache_res_fmt_or_def
(result
, format
, default
) {
62 return result
["is_expired"] ? default
: sprintf(format
, result
["value"])
65 function cache_get_fmt_def
(src
, key
, ttl
, format
, default
, result
) {
66 default = default ? default
: "--"
67 cache_get
(result
, src
, key
, ttl
)
68 return cache_res_fmt_or_def
(result
, format
, default
)
71 function cache_get_time
( src
, key
, time
) {
72 src =
"khatus_sensor_datetime"
74 time = _cache
[src
, key
]
75 _cache_atime
[src
, key
] = time
79 function cache_gc
( src_and_key
, parts
, src
, key
, unused_for
) {
80 for (src_and_key in _cache
) {
81 split(src_and_key
, parts
, SUBSEP
)
84 val = _cache
[src
, key
]
85 unused_for = cache_get_time
() - _cache_atime
[src
, key
]
86 if (unused_for
> GC_Interval
) {
90 "Deleting unused data SRC=%s KEY=%s VAL=%s",
94 delete _cache
[src
, key
]
99 function cache_maybe_total_to_diff
(src
, key
, val
, key_parts
) {
100 split(key
, key_parts
, Kfs
)
101 if (_total_to_diff
[src
, key_parts
[1]]) {
102 _prev
[src
, key
] = _curr
[src
, key
]
103 _curr
[src
, key
] = val
104 return (_curr
[src
, key
] - _prev
[src
, key
])
110 function cache_maybe_scale
(src
, key
, val
, key_parts
) {
111 split(key
, key_parts
, Kfs
)
112 if ((src SUBSEP key_parts
[1]) in _scale
) {
113 return val
* _scale
[src
, key_parts
[1]]