| 1 | # Naming convention: |
| 2 | # Variables: |
| 3 | # - global, builtin : ALLCAPS |
| 4 | # - global, public : Camel_Snake_Man_Bear_Pig |
| 5 | # - global, private : _snake_case_prefixed_underscore |
| 6 | # - local : snake_case |
| 7 | # Functions: |
| 8 | # - global, public : snake_case |
| 9 | |
| 10 | BEGIN { |
| 11 | GC_Interval = GC_Interval ? GC_Interval : 3600 # seconds |
| 12 | |
| 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 |
| 17 | |
| 18 | # (x * y) / z = x * w |
| 19 | # ==> w = y / z |
| 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 |
| 26 | # (x / y) = x * z |
| 27 | # ==> z = 1 / y |
| 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 |
| 32 | } |
| 33 | |
| 34 | function cache_update(node, module, key, val, time) { |
| 35 | # TODO: Use node value |
| 36 | val = cache_maybe_total_to_diff(module, key, val) |
| 37 | val = cache_maybe_scale(module, key, val) |
| 38 | _cache[module, key] = val |
| 39 | time = cache_get_time() |
| 40 | _cache_mtime[module, key] = time |
| 41 | if (time % GC_Interval == 0) { |
| 42 | cache_gc() |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | function cache_get(result, module, key, ttl, time, age, is_expired) { |
| 47 | time = cache_get_time() |
| 48 | _cache_atime[module, key] = time |
| 49 | age = time - _cache_mtime[module, key] |
| 50 | result["is_expired"] = ttl && age > ttl # ttl = 0 => forever |
| 51 | result["value"] = _cache[module, key] |
| 52 | } |
| 53 | |
| 54 | function cache_res_fmt_or_def(result, format, default) { |
| 55 | return result["is_expired"] ? default : sprintf(format, result["value"]) |
| 56 | } |
| 57 | |
| 58 | function cache_get_fmt_def(module, key, ttl, format, default, result) { |
| 59 | default = default ? default : "--" |
| 60 | cache_get(result, module, key, ttl) |
| 61 | return cache_res_fmt_or_def(result, format, default) |
| 62 | } |
| 63 | |
| 64 | function cache_get_time( module, key, time) { |
| 65 | module = "khatus_sensor_datetime" |
| 66 | key = "epoch" |
| 67 | time = _cache[module, key] |
| 68 | _cache_atime[module, key] = time |
| 69 | return time |
| 70 | } |
| 71 | |
| 72 | function cache_gc( module_and_key, parts, module, key, unused_for) { |
| 73 | for (module_and_key in _cache) { |
| 74 | split(module_and_key, parts, SUBSEP) |
| 75 | module = parts[1] |
| 76 | key = parts[2] |
| 77 | val = _cache[module, key] |
| 78 | unused_for = cache_get_time() - _cache_atime[module, key] |
| 79 | if (unused_for > GC_Interval) { |
| 80 | msg_out_log_info(\ |
| 81 | "cache_gc", |
| 82 | sprintf(\ |
| 83 | "Deleting unused data MODULE=%s KEY=%s VAL=%s", |
| 84 | module, key, val\ |
| 85 | ) \ |
| 86 | ) |
| 87 | delete _cache[module, key] |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | function cache_maybe_total_to_diff(module, key, val, key_parts) { |
| 93 | split(key, key_parts, Kfs) |
| 94 | if (_total_to_diff[module, key_parts[1]]) { |
| 95 | _prev[module, key] = _curr[module, key] |
| 96 | _curr[module, key] = val |
| 97 | return (_curr[module, key] - _prev[module, key]) |
| 98 | } else { |
| 99 | return val |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | function cache_maybe_scale(module, key, val, key_parts) { |
| 104 | split(key, key_parts, Kfs) |
| 105 | if ((module SUBSEP key_parts[1]) in _scale) { |
| 106 | return val * _scale[module, key_parts[1]] |
| 107 | } else { |
| 108 | return val |
| 109 | } |
| 110 | } |