Necesitaba cargar en forma masiva el monitoreo de cientos de dispositivos o host en un icinga2 con el modulo director para su configuración, este modulo no cuenta con carga masiva, como la bases de datos era postgresql, cree una función, donde tenia como datos de ingreso: el nombre del host, la ip del host, el host grupo al cual pertenece, por defecto se asocia al template 1 del host.
Todo esto es en la base de datos del director: los host se almacenan en la tabla icinga_host, en icinga_hostgroup_host se asocia el grupo con el host, en icinga_hostgroup_host_resolved se almacena la referencia al grupo y en icinga_host_inheritance se asocia el template.
DROP FUNCTION carga(nombre varchar, ip varchar,grupo varchar);
CREATE FUNCTION carga(nombre varchar, ip varchar,grupo varchar ) RETURNS integer AS $$
DECLARE
secuencia INTEGER;
grupoid INTEGER;
BEGIN
INSERT INTO icinga_host (object_name, object_type, disabled, display_name, address, address6, check_command_id, max_check_attempts, check_period_id, check_interval, retry_interval, check_timeout, enable_notifications, enable_active_checks, enable_passive_checks, enable_event_handler, enable_flapping, enable_perfdata, event_command_id, flapping_threshold_high, flapping_threshold_low, volatile, zone_id, command_endpoint_id, notes, notes_url, action_url, icon_image, icon_image_alt, has_agent, master_should_connect, accept_config, api_key, template_choice_id) VALUES (nombre, 'object', 'n', NULL, ip, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
SELECT CURRVAL('icinga_host_id_seq') into secuencia;
SELECT id into grupoid from icinga_hostgroup where object_name=grupo;
EXECUTE 'INSERT INTO icinga_hostgroup_host (host_id, hostgroup_id) VALUES ($1,$2)' USING secuencia, grupoid;
EXECUTE 'INSERT INTO icinga_hostgroup_host_resolved (hostgroup_id, host_id) VALUES ($2, $1)' USING secuencia, grupoid;
EXECUTE 'INSERT INTO icinga_host_inheritance (host_id, parent_host_id, weight) VALUES ($1, 1, 1)' USING secuencia;
RETURN secuencia;
END; $$
LANGUAGE PLPGSQL;
Luego en un planilla genere los sql ue llamaban a la función de la forma
select carga('equipo1','ip1','grupo');
y así sucesivamente.