|
| 1 | +import { ExtendedTool, ToolHandlers } from '../../utils/types' |
| 2 | +import { v1 } from '@datadog/datadog-api-client' |
| 3 | +import { createToolSchema } from '../../utils/tool' |
| 4 | +import { |
| 5 | + ListHostsZodSchema, |
| 6 | + GetActiveHostsCountZodSchema, |
| 7 | + MuteHostZodSchema, |
| 8 | + UnmuteHostZodSchema, |
| 9 | +} from './schema' |
| 10 | +import { datadogConfig } from '../../utils/datadog' |
| 11 | + |
| 12 | +/** |
| 13 | + * This module implements Datadog host management tools for muting, unmuting, |
| 14 | + * and retrieving host information using the Datadog API client. |
| 15 | + */ |
| 16 | + |
| 17 | +/** Available host management tool names */ |
| 18 | +type HostsToolName = |
| 19 | + | 'list_hosts' |
| 20 | + | 'get_active_hosts_count' |
| 21 | + | 'mute_host' |
| 22 | + | 'unmute_host' |
| 23 | +/** Extended tool type with host-specific operations */ |
| 24 | +type HostsTool = ExtendedTool<HostsToolName> |
| 25 | + |
| 26 | +/** |
| 27 | + * Array of available host management tools. |
| 28 | + * Each tool is created with a schema for input validation and includes a description. |
| 29 | + */ |
| 30 | +export const HOSTS_TOOLS: HostsTool[] = [ |
| 31 | + createToolSchema(MuteHostZodSchema, 'mute_host', 'Mute a host in Datadog'), |
| 32 | + createToolSchema( |
| 33 | + UnmuteHostZodSchema, |
| 34 | + 'unmute_host', |
| 35 | + 'Unmute a host in Datadog', |
| 36 | + ), |
| 37 | + createToolSchema( |
| 38 | + ListHostsZodSchema, |
| 39 | + 'list_hosts', |
| 40 | + 'Get list of hosts from Datadog', |
| 41 | + ), |
| 42 | + createToolSchema( |
| 43 | + GetActiveHostsCountZodSchema, |
| 44 | + 'get_active_hosts_count', |
| 45 | + 'Get the total number of active hosts in Datadog (defaults to last 5 minutes)', |
| 46 | + ), |
| 47 | +] as const |
| 48 | + |
| 49 | +/** Datadog API client instance for host operations */ |
| 50 | +const API_INSTANCE = new v1.HostsApi(datadogConfig) |
| 51 | + |
| 52 | +/** Type definition for host management tool implementations */ |
| 53 | +type HostsToolHandlers = ToolHandlers<HostsToolName> |
| 54 | + |
| 55 | +/** |
| 56 | + * Implementation of host management tool handlers. |
| 57 | + * Each handler validates inputs using Zod schemas and interacts with the Datadog API. |
| 58 | + */ |
| 59 | +export const HOSTS_HANDLERS: HostsToolHandlers = { |
| 60 | + /** |
| 61 | + * Mutes a specified host in Datadog. |
| 62 | + * Silences alerts and notifications for the host until unmuted or until the specified end time. |
| 63 | + */ |
| 64 | + mute_host: async (request) => { |
| 65 | + const { hostname, message, end, override } = MuteHostZodSchema.parse( |
| 66 | + request.params.arguments, |
| 67 | + ) |
| 68 | + |
| 69 | + await API_INSTANCE.muteHost({ |
| 70 | + hostName: hostname, |
| 71 | + body: { |
| 72 | + message, |
| 73 | + end, |
| 74 | + override, |
| 75 | + }, |
| 76 | + }) |
| 77 | + |
| 78 | + return { |
| 79 | + content: [ |
| 80 | + { |
| 81 | + type: 'text', |
| 82 | + text: JSON.stringify( |
| 83 | + { |
| 84 | + status: 'success', |
| 85 | + message: `Host ${hostname} has been muted successfully${message ? ` with message: ${message}` : ''}${end ? ` until ${new Date(end * 1000).toISOString()}` : ''}`, |
| 86 | + }, |
| 87 | + null, |
| 88 | + 2, |
| 89 | + ), |
| 90 | + }, |
| 91 | + ], |
| 92 | + } |
| 93 | + }, |
| 94 | + |
| 95 | + /** |
| 96 | + * Unmutes a previously muted host in Datadog. |
| 97 | + * Re-enables alerts and notifications for the specified host. |
| 98 | + */ |
| 99 | + unmute_host: async (request) => { |
| 100 | + const { hostname } = UnmuteHostZodSchema.parse(request.params.arguments) |
| 101 | + |
| 102 | + await API_INSTANCE.unmuteHost({ |
| 103 | + hostName: hostname, |
| 104 | + }) |
| 105 | + |
| 106 | + return { |
| 107 | + content: [ |
| 108 | + { |
| 109 | + type: 'text', |
| 110 | + text: JSON.stringify( |
| 111 | + { |
| 112 | + status: 'success', |
| 113 | + message: `Host ${hostname} has been unmuted successfully`, |
| 114 | + }, |
| 115 | + null, |
| 116 | + 2, |
| 117 | + ), |
| 118 | + }, |
| 119 | + ], |
| 120 | + } |
| 121 | + }, |
| 122 | + |
| 123 | + /** |
| 124 | + * Retrieves counts of active and up hosts in Datadog. |
| 125 | + * Provides total counts of hosts that are reporting and operational. |
| 126 | + */ |
| 127 | + get_active_hosts_count: async (request) => { |
| 128 | + const { from } = GetActiveHostsCountZodSchema.parse( |
| 129 | + request.params.arguments, |
| 130 | + ) |
| 131 | + |
| 132 | + const response = await API_INSTANCE.getHostTotals({ |
| 133 | + from, |
| 134 | + }) |
| 135 | + |
| 136 | + return { |
| 137 | + content: [ |
| 138 | + { |
| 139 | + type: 'text', |
| 140 | + text: JSON.stringify( |
| 141 | + { |
| 142 | + total_active: response.totalActive || 0, // Total number of active hosts (UP and reporting) to Datadog |
| 143 | + total_up: response.totalUp || 0, // Number of hosts that are UP and reporting to Datadog |
| 144 | + }, |
| 145 | + null, |
| 146 | + 2, |
| 147 | + ), |
| 148 | + }, |
| 149 | + ], |
| 150 | + } |
| 151 | + }, |
| 152 | + |
| 153 | + /** |
| 154 | + * Lists and filters hosts monitored by Datadog. |
| 155 | + * Supports comprehensive querying with filtering, sorting, and pagination. |
| 156 | + * Returns detailed host information including status, metadata, and monitoring data. |
| 157 | + */ |
| 158 | + list_hosts: async (request) => { |
| 159 | + const { |
| 160 | + filter, |
| 161 | + sort_field, |
| 162 | + sort_dir, |
| 163 | + start, |
| 164 | + count, |
| 165 | + from, |
| 166 | + include_muted_hosts_data, |
| 167 | + include_hosts_metadata, |
| 168 | + } = ListHostsZodSchema.parse(request.params.arguments) |
| 169 | + |
| 170 | + const response = await API_INSTANCE.listHosts({ |
| 171 | + filter, |
| 172 | + sortField: sort_field, |
| 173 | + sortDir: sort_dir, |
| 174 | + start, |
| 175 | + count, |
| 176 | + from, |
| 177 | + includeMutedHostsData: include_muted_hosts_data, |
| 178 | + includeHostsMetadata: include_hosts_metadata, |
| 179 | + }) |
| 180 | + |
| 181 | + if (!response.hostList) { |
| 182 | + throw new Error('No hosts data returned') |
| 183 | + } |
| 184 | + |
| 185 | + // Transform API response into a more convenient format |
| 186 | + const hosts = response.hostList.map((host) => ({ |
| 187 | + name: host.name, |
| 188 | + id: host.id, |
| 189 | + aliases: host.aliases, |
| 190 | + apps: host.apps, |
| 191 | + mute: host.isMuted, |
| 192 | + last_reported: host.lastReportedTime, |
| 193 | + meta: host.meta, |
| 194 | + metrics: host.metrics, |
| 195 | + sources: host.sources, |
| 196 | + up: host.up, |
| 197 | + url: `https://${datadogConfig.site}/infrastructure?host=${host.name}`, |
| 198 | + })) |
| 199 | + |
| 200 | + return { |
| 201 | + content: [ |
| 202 | + { |
| 203 | + type: 'text', |
| 204 | + text: `Hosts: ${JSON.stringify(hosts)}`, |
| 205 | + }, |
| 206 | + ], |
| 207 | + } |
| 208 | + }, |
| 209 | +} as const |
0 commit comments