| 1 |
// JSON tapset. |
| 2 |
// Copyright (C) 2015 Red Hat Inc. |
| 3 |
// |
| 4 |
// This file is part of systemtap, and is free software. You can |
| 5 |
// redistribute it and/or modify it under the terms of the GNU General |
| 6 |
// Public License (GPL); either version 2, or (at your option) any |
| 7 |
// later version. |
| 8 |
// |
| 9 |
// <tapsetdescription> |
| 10 |
// The JSON tapset provides probes, functions, and macros to generate |
| 11 |
// a JSON metadata and data file. The JSON metadata file is located in |
| 12 |
// /proc/systemtap/MODULE/metadata.json. The JSON data file is located |
| 13 |
// in /proc/systemtap/MODULE/data.json. The JSON data file is updated |
| 14 |
// with current data every time the file is read. |
| 15 |
// </tapsetdescription> |
| 16 |
|
| 17 |
|
| 18 |
@__private30 global __json_prefix |
| 19 |
@__private30 global __json_metric_type, __json_metric_desc |
| 20 |
@__private30 global __json_metric_units, __json_metric_pointer |
| 21 |
@__private30 global __json_array_metric_type, __json_array_metric_desc |
| 22 |
@__private30 global __json_array_metric_units, __json_array_metric_pointer |
| 23 |
global __json_metric_output, __json_array_output |
| 24 |
|
| 25 |
probe init |
| 26 |
{ |
| 27 |
// Make sure the systemtap translator knows what types the globals are. |
| 28 |
__json_prefix = "" |
| 29 |
|
| 30 |
__json_metric_units[""] = "" |
| 31 |
delete __json_metric_units |
| 32 |
|
| 33 |
__json_array_metric_type["", ""] = "" |
| 34 |
delete __json_array_metric_type |
| 35 |
|
| 36 |
__json_array_metric_desc["", ""] = "" |
| 37 |
delete __json_array_metric_desc |
| 38 |
|
| 39 |
__json_array_metric_units["", ""] = "" |
| 40 |
delete __json_array_metric_units |
| 41 |
|
| 42 |
__json_array_metric_pointer["", ""] = "" |
| 43 |
delete __json_array_metric_pointer |
| 44 |
|
| 45 |
__json_array_output[""] = 1 |
| 46 |
delete __json_array_output |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* sfunction json_set_prefix - Set the metric prefix. |
| 51 |
* |
| 52 |
* @prefix: The prefix name to be used. |
| 53 |
* |
| 54 |
* Description: This function sets the "prefix", which is the name |
| 55 |
* of the base of the metric hierarchy. Calling this function is |
| 56 |
* optional, by default the name of the systemtap module is used. |
| 57 |
*/ |
| 58 |
function json_set_prefix:long(prefix:string) |
| 59 |
{ |
| 60 |
__json_prefix = prefix |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* sfunction json_add_numeric_metric - Add a numeric metric |
| 65 |
* |
| 66 |
* @name: The name of the numeric metric. |
| 67 |
* @description: Metric description. An empty string can be used. |
| 68 |
* @units: Metic units. An empty string can be used. |
| 69 |
* |
| 70 |
* Description: This function adds a numeric metric, setting up |
| 71 |
* everything needed. |
| 72 |
*/ |
| 73 |
function json_add_numeric_metric:long(name:string, description:string, units:string) |
| 74 |
{ |
| 75 |
if (name in __json_metric_type) |
| 76 |
error(sprintf("Metric '%s' already exists", name)) |
| 77 |
__json_metric_type[name] = "integer" |
| 78 |
__json_metric_desc[name] = description |
| 79 |
__json_metric_units[name] = units |
| 80 |
# FIXME: Do we need to validate the name? For instance, what if it |
| 81 |
# had a double-quote character in it? |
| 82 |
__json_metric_pointer[name] = "/" . name |
| 83 |
return 0 |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* sfunction json_add_string_metric - Add a string metric |
| 88 |
* |
| 89 |
* @name: The name of the string metric. |
| 90 |
* @description: Metric description. An empty string can be used. |
| 91 |
* |
| 92 |
* Description: This function adds a string metric, setting up |
| 93 |
* everything needed. |
| 94 |
*/ |
| 95 |
function json_add_string_metric:long(name:string, description:string) |
| 96 |
{ |
| 97 |
if (name in __json_metric_type) |
| 98 |
error(sprintf("Metric '%s' already exists", name)) |
| 99 |
__json_metric_type[name] = "string" |
| 100 |
__json_metric_desc[name] = description |
| 101 |
__json_metric_pointer[name] = "/" . name |
| 102 |
return 0 |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* sfunction json_add_array - Add an array |
| 107 |
* |
| 108 |
* @name: The name of the array. |
| 109 |
* @description: Array description. An empty string can be used. |
| 110 |
* |
| 111 |
* Description: This function adds a array, setting up everything |
| 112 |
* needed. Arrays contain other metrics, added with |
| 113 |
* json_add_array_numeric_metric() or json_add_array_string_metric(). |
| 114 |
*/ |
| 115 |
function json_add_array:long(name:string, description:string) |
| 116 |
{ |
| 117 |
if (name in __json_metric_type) |
| 118 |
error(sprintf("Metric '%s' already exists", name)) |
| 119 |
__json_metric_type[name] = "array" |
| 120 |
__json_metric_desc[name] = description |
| 121 |
__json_metric_pointer[name] = "/" . name |
| 122 |
|
| 123 |
# Go ahead and add "__id", which is the array index. |
| 124 |
json_add_array_string_metric(name, "__id", "") |
| 125 |
return 0 |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* sfunction json_add_array_numeric_metric - Add a numeric metric to an array |
| 130 |
* |
| 131 |
* @array_name: The name of the array the numeric metric should be |
| 132 |
* added to. |
| 133 |
* @metric_name: The name of the numeric metric. |
| 134 |
* @metric_description: Metric description. An empty string can be used. |
| 135 |
* @metric_units: Metic units. An empty string can be used. |
| 136 |
* |
| 137 |
* Description: This function adds a numeric metric to an array, |
| 138 |
* setting up everything needed. |
| 139 |
*/ |
| 140 |
function json_add_array_numeric_metric:long(array_name:string, metric_name:string, metric_description:string, metric_units:string) |
| 141 |
{ |
| 142 |
if ([array_name, metric_name] in __json_array_metric_type) |
| 143 |
error(sprintf("Array metric '%s' already exists in array %s", metric_name, |
| 144 |
array_name)) |
| 145 |
__json_array_metric_pointer[array_name, metric_name] |
| 146 |
= sprintf("/%s", metric_name) |
| 147 |
__json_array_metric_type[array_name, metric_name] = "integer" |
| 148 |
__json_array_metric_desc[array_name, metric_name] = metric_description |
| 149 |
__json_array_metric_units[array_name, metric_name] = metric_units |
| 150 |
return 0 |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* sfunction json_add_array_string_metric - Add a string metric to an array |
| 155 |
* |
| 156 |
* @array_name: The name of the array the string metric should be |
| 157 |
* added to. |
| 158 |
* @metric_name: The name of the string metric. |
| 159 |
* @metric_description: Metric description. An empty string can be used. |
| 160 |
* |
| 161 |
* Description: This function adds a string metric to an array, |
| 162 |
* setting up everything needed. |
| 163 |
*/ |
| 164 |
function json_add_array_string_metric:long(array_name:string, metric_name:string, metric_description:string) |
| 165 |
{ |
| 166 |
if ([array_name, metric_name] in __json_array_metric_type) |
| 167 |
error(sprintf("Array metric '%s' already exists in array %s", metric_name, |
| 168 |
array_name)) |
| 169 |
__json_array_metric_pointer[array_name, metric_name] |
| 170 |
= sprintf("/%s", metric_name) |
| 171 |
__json_array_metric_type[array_name, metric_name] = "string" |
| 172 |
__json_array_metric_desc[array_name, metric_name] = metric_description |
| 173 |
return 0 |
| 174 |
} |
| 175 |
|
| 176 |
probe procfs("metadata.json").read.maxsize(8192) |
| 177 |
{ |
| 178 |
# Note: This is the "pretty-printed" version, intended to be read by |
| 179 |
# humans. We could remove the whitespace and newlines if we wanted |
| 180 |
# to make the output shorter (but less readable). |
| 181 |
# |
| 182 |
# Note 2: Note that we have to break this long string into more than |
| 183 |
# 1 assignment since we're bumping up against MAXSTRINGLEN. Procfs |
| 184 |
# $value can hold more than MAXSTRINGLEN because of the |
| 185 |
# '.maxsize(N)' parameter. |
| 186 |
$value = "{\n" |
| 187 |
|
| 188 |
if (__json_prefix != "") |
| 189 |
$value .= sprintf(" \"prefix\": \"%s\",\n", __json_prefix) |
| 190 |
|
| 191 |
$value .= " \"metrics\": [\n" |
| 192 |
|
| 193 |
__comma_needed = 0 |
| 194 |
foreach (__name in __json_metric_type) { |
| 195 |
if (__comma_needed) |
| 196 |
$value .= ",\n" |
| 197 |
__comma_needed = 1 |
| 198 |
|
| 199 |
if (__json_metric_type[__name] != "array") { |
| 200 |
@__json_output_metric_metadata(" ", __name, |
| 201 |
__json_metric_pointer[__name], |
| 202 |
__json_metric_type[__name], |
| 203 |
__json_metric_desc[__name], |
| 204 |
__json_metric_units[__name]) |
| 205 |
} |
| 206 |
else { |
| 207 |
# For an array, we have to output the information about the |
| 208 |
# array itself, then output each field in the array. |
| 209 |
$value .= " \{\n" |
| 210 |
$value .= sprintf(" \"name\": \"%s\",\n", __name) |
| 211 |
$value .= sprintf(" \"pointer\": \"%s\",\n", |
| 212 |
__json_metric_pointer[__name]) |
| 213 |
$value .= " \"type\": \"array\",\n" |
| 214 |
if (strlen(__json_metric_desc[__name]) > 0) |
| 215 |
$value .= sprintf(" \"description\": \"%s\",\n", |
| 216 |
__json_metric_desc[__name]) |
| 217 |
$value .= " \"index\": \"/__id\",\n" |
| 218 |
$value .= " \"metrics\": [\n" |
| 219 |
__array_comma_needed = 0 |
| 220 |
|
| 221 |
# Output each field in the array. |
| 222 |
foreach ([__array_name, __metric_name] in __json_array_metric_type) { |
| 223 |
if (__name == __array_name) { |
| 224 |
# Skip '__id', the array index. |
| 225 |
if (__metric_name == "__id") |
| 226 |
continue |
| 227 |
|
| 228 |
if (__array_comma_needed) |
| 229 |
$value .= ",\n" |
| 230 |
__array_comma_needed = 1 |
| 231 |
|
| 232 |
__subpointer = __json_array_metric_pointer[__array_name, __metric_name] |
| 233 |
__subtype = __json_array_metric_type[__array_name, __metric_name] |
| 234 |
__subdesc = __json_array_metric_desc[__array_name, __metric_name] |
| 235 |
__subunits = __json_array_metric_units[__array_name, __metric_name] |
| 236 |
|
| 237 |
@__json_output_metric_metadata(" ", __metric_name, |
| 238 |
__subpointer, __subtype, |
| 239 |
__subdesc, __subunits) |
| 240 |
} |
| 241 |
} |
| 242 |
$value .= |
| 243 |
"\n" |
| 244 |
" ]\n" |
| 245 |
" }" |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
$value .= |
| 250 |
"\n" |
| 251 |
" ]\n" |
| 252 |
"}\n" |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* probe json_data - Fires whenever JSON data is wanted by a reader. |
| 257 |
* |
| 258 |
* Context: |
| 259 |
* This probe fires when the JSON data is about to be read. This |
| 260 |
* probe must gather up data and then call the following macros to |
| 261 |
* output the data in JSON format. First, @json_output_data_start() |
| 262 |
* must be called. That call is followed by one or more of the |
| 263 |
* following (one call for each data item): |
| 264 |
* @json_output_string_value(), @json_output_numeric_value(), |
| 265 |
* @json_output_array_string_value(), and |
| 266 |
* @json_output_array_numeric_value(). Finally @json_output_data_end() |
| 267 |
* must be called. |
| 268 |
*/ |
| 269 |
probe json_data = procfs("data.json").read.maxsize(8192) |
| 270 |
{ |
| 271 |
} |