| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "mcp-tools-configured-alerts.h" |
| 4 | #include "mcp-params.h" |
| 5 | #include "health/health_internals.h" |
| 6 | |
| 7 | // Schema for list_configured_alerts - no parameters |
| 8 | void mcp_tool_list_configured_alerts_schema(BUFFER *buffer) { |
| 9 | // Tool metadata |
| 10 | buffer_json_member_add_object(buffer, "inputSchema"); |
| 11 | buffer_json_member_add_string(buffer, "type", "object"); |
| 12 | buffer_json_member_add_string(buffer, "title", "List configured alerts"); |
| 13 | |
| 14 | buffer_json_member_add_object(buffer, "properties"); |
| 15 | // No properties - this tool accepts no parameters |
| 16 | buffer_json_object_close(buffer); // properties |
| 17 | |
| 18 | buffer_json_object_close(buffer); // inputSchema |
| 19 | } |
| 20 | |
| 21 | // Execute list_configured_alerts - no filtering, returns all prototypes |
| 22 | MCP_RETURN_CODE mcp_tool_list_configured_alerts_execute(MCP_CLIENT *mcpc, struct json_object *params __maybe_unused, MCP_REQUEST_ID id __maybe_unused) { |
| 23 | if (!mcpc) |
| 24 | return MCP_RC_ERROR; |
| 25 | |
| 26 | // Create a temporary buffer for the result |
| 27 | CLEAN_BUFFER *t = buffer_create(0, NULL); |
| 28 | size_t count = 0; |
| 29 | |
| 30 | // Build the JSON response in tabular format |
| 31 | buffer_json_initialize(t, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_MINIFY); |
| 32 | buffer_json_member_add_uint64(t, "format_version", 1); |
| 33 | |
| 34 | // Add header for tabular data |
| 35 | buffer_json_member_add_array(t, "configured_alerts_header"); |
| 36 | { |
| 37 | buffer_json_add_array_item_string(t, "name"); |
| 38 | buffer_json_add_array_item_string(t, "applies_to"); |
| 39 | buffer_json_add_array_item_string(t, "on"); |
| 40 | buffer_json_add_array_item_string(t, "summary"); |
| 41 | buffer_json_add_array_item_string(t, "component"); |
| 42 | buffer_json_add_array_item_string(t, "classification"); |
| 43 | buffer_json_add_array_item_string(t, "type"); |
| 44 | buffer_json_add_array_item_string(t, "recipient"); |
| 45 | } |
| 46 | buffer_json_array_close(t); // configured_alerts_header |
| 47 | |
| 48 | // Add tabular data |
| 49 | buffer_json_member_add_array(t, "configured_alerts"); |
| 50 | { |
| 51 | // Iterate through all alert prototypes |
| 52 | RRD_ALERT_PROTOTYPE *ap; |
| 53 | dfe_start_read(health_globals.prototypes.dict, ap) { |
| 54 | // Only use the first rule for each prototype |
| 55 | if (ap) { |
| 56 | buffer_json_add_array_item_array(t); // Start row |
| 57 | { |
| 58 | // name |
| 59 | buffer_json_add_array_item_string(t, string2str(ap->config.name)); |
| 60 | |
| 61 | // alert_type (template or alarm) |
| 62 | buffer_json_add_array_item_string(t, ap->match.is_template ? "context" : "instance"); |
| 63 | |
| 64 | // on (context or instance) |
| 65 | if(ap->match.is_template) |
| 66 | buffer_json_add_array_item_string(t, string2str(ap->match.on.context)); |
| 67 | else |
| 68 | buffer_json_add_array_item_string(t, string2str(ap->match.on.chart)); |
| 69 | |
| 70 | // summary |
| 71 | buffer_json_add_array_item_string(t, string2str(ap->config.summary)); |
| 72 | |
| 73 | // component |
| 74 | buffer_json_add_array_item_string(t, string2str(ap->config.component)); |
| 75 | |
| 76 | // classification |
| 77 | buffer_json_add_array_item_string(t, string2str(ap->config.classification)); |
| 78 | |
| 79 | // type |
| 80 | buffer_json_add_array_item_string(t, string2str(ap->config.type)); |
| 81 | |
| 82 | // recipient |
| 83 | buffer_json_add_array_item_string(t, string2str(ap->config.recipient)); |
| 84 | } |
| 85 | buffer_json_array_close(t); // End row |
| 86 | |
| 87 | count++; |
| 88 | } |
| 89 | } |
| 90 | dfe_done(ap); |
| 91 | } |
| 92 | buffer_json_array_close(t); // configured_alerts |
| 93 | |
| 94 | // Add summary |
| 95 | buffer_json_member_add_object(t, "summary"); |
| 96 | { |
| 97 | buffer_json_member_add_uint64(t, "total_prototypes", count); |
| 98 | } |
| 99 | buffer_json_object_close(t); // summary |
| 100 | |
| 101 | buffer_json_finalize(t); |
| 102 | |
| 103 | // Initialize success response |
| 104 | mcp_init_success_result(mcpc, id); |
| 105 | |
| 106 | // Start building a content array for the result |
| 107 | buffer_json_member_add_array(mcpc->result, "content"); |
| 108 | { |
| 109 | // Return text content for LLM compatibility |
| 110 | buffer_json_add_array_item_object(mcpc->result); |
| 111 | { |
| 112 | buffer_json_member_add_string(mcpc->result, "type", "text"); |
| 113 | buffer_json_member_add_string(mcpc->result, "text", buffer_tostring(t)); |
| 114 | } |
| 115 | buffer_json_object_close(mcpc->result); // Close text content |
| 116 | } |
| 117 | buffer_json_array_close(mcpc->result); // Close content array |
| 118 | buffer_json_object_close(mcpc->result); // Close result object |
| 119 | buffer_json_finalize(mcpc->result); // Finalize the JSON |
| 120 | |
| 121 | return MCP_RC_OK; |
| 122 | } |