summaryrefslogtreecommitdiffstats
path: root/lib/common/alerts.c
blob: 98b1e3facdec0ef4ed6708be61acce823c95d723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright 2015-2023 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU Lesser General Public License
 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
 */

#include <crm_internal.h>
#include <crm/crm.h>
#include <crm/lrmd.h>
#include <crm/msg_xml.h>
#include <crm/common/alerts_internal.h>
#include <crm/common/cib_internal.h>
#include <crm/common/xml_internal.h>

/*
 * to allow script compatibility we can have more than one
 * set of environment variables
 */
const char *pcmk__alert_keys[PCMK__ALERT_INTERNAL_KEY_MAX][3] =
{
    [PCMK__alert_key_recipient] = {
        "CRM_notify_recipient",         "CRM_alert_recipient",          NULL
    },
    [PCMK__alert_key_node] = {
        "CRM_notify_node",              "CRM_alert_node",               NULL
    },
    [PCMK__alert_key_nodeid] = {
        "CRM_notify_nodeid",            "CRM_alert_nodeid",             NULL
    },
    [PCMK__alert_key_rsc] = {
        "CRM_notify_rsc",               "CRM_alert_rsc",                NULL
    },
    [PCMK__alert_key_task] = {
        "CRM_notify_task",              "CRM_alert_task",               NULL
    },
    [PCMK__alert_key_interval] = {
        "CRM_notify_interval",          "CRM_alert_interval",           NULL
    },
    [PCMK__alert_key_desc] = {
        "CRM_notify_desc",              "CRM_alert_desc",               NULL
    },
    [PCMK__alert_key_status] = {
        "CRM_notify_status",            "CRM_alert_status",             NULL
    },
    [PCMK__alert_key_target_rc] = {
        "CRM_notify_target_rc",         "CRM_alert_target_rc",          NULL
    },
    [PCMK__alert_key_rc] = {
        "CRM_notify_rc",                "CRM_alert_rc",                 NULL
    },
    [PCMK__alert_key_kind] = {
        "CRM_notify_kind",              "CRM_alert_kind",               NULL
    },
    [PCMK__alert_key_version] = {
        "CRM_notify_version",           "CRM_alert_version",            NULL
    },
    [PCMK__alert_key_node_sequence] = {
        "CRM_notify_node_sequence",     PCMK__ALERT_NODE_SEQUENCE,      NULL
    },
    [PCMK__alert_key_timestamp] = {
        "CRM_notify_timestamp",         "CRM_alert_timestamp",          NULL
    },
    [PCMK__alert_key_attribute_name] = {
        "CRM_notify_attribute_name",    "CRM_alert_attribute_name",     NULL
    },
    [PCMK__alert_key_attribute_value] = {
        "CRM_notify_attribute_value",   "CRM_alert_attribute_value",    NULL
    },
    [PCMK__alert_key_timestamp_epoch] = {
        "CRM_notify_timestamp_epoch",   "CRM_alert_timestamp_epoch",    NULL
    },
    [PCMK__alert_key_timestamp_usec] = {
        "CRM_notify_timestamp_usec",    "CRM_alert_timestamp_usec",     NULL
    },
    [PCMK__alert_key_exec_time] = {
        "CRM_notify_exec_time",         "CRM_alert_exec_time",          NULL
    }
};

/*!
 * \brief Create a new alert entry structure
 *
 * \param[in] id  ID to use
 * \param[in] path  Path to alert agent executable
 *
 * \return Pointer to newly allocated alert entry
 * \note Non-string fields will be filled in with defaults.
 *       It is the caller's responsibility to free the result,
 *       using pcmk__free_alert().
 */
pcmk__alert_t *
pcmk__alert_new(const char *id, const char *path)
{
    pcmk__alert_t *entry = calloc(1, sizeof(pcmk__alert_t));

    CRM_ASSERT(entry && id && path);
    entry->id = strdup(id);
    entry->path = strdup(path);
    entry->timeout = PCMK__ALERT_DEFAULT_TIMEOUT_MS;
    entry->flags = pcmk__alert_default;
    return entry;
}

void
pcmk__free_alert(pcmk__alert_t *entry)
{
    if (entry) {
        free(entry->id);
        free(entry->path);
        free(entry->tstamp_format);
        free(entry->recipient);

        g_strfreev(entry->select_attribute_name);
        if (entry->envvars) {
            g_hash_table_destroy(entry->envvars);
        }
        free(entry);
    }
}

/*!
 * \internal
 * \brief Duplicate an alert entry
 *
 * \param[in] entry  Alert entry to duplicate
 *
 * \return Duplicate of alert entry
 */
pcmk__alert_t *
pcmk__dup_alert(const pcmk__alert_t *entry)
{
    pcmk__alert_t *new_entry = pcmk__alert_new(entry->id, entry->path);

    new_entry->timeout = entry->timeout;
    new_entry->flags = entry->flags;
    new_entry->envvars = pcmk__str_table_dup(entry->envvars);
    pcmk__str_update(&new_entry->tstamp_format, entry->tstamp_format);
    pcmk__str_update(&new_entry->recipient, entry->recipient);
    if (entry->select_attribute_name) {
        new_entry->select_attribute_name = g_strdupv(entry->select_attribute_name);
    }
    return new_entry;
}

void
pcmk__add_alert_key(GHashTable *table, enum pcmk__alert_keys_e name,
                    const char *value)
{
    for (const char **key = pcmk__alert_keys[name]; *key; key++) {
        crm_trace("Inserting alert key %s = '%s'", *key, value);
        if (value) {
            g_hash_table_insert(table, strdup(*key), strdup(value));
        } else {
            g_hash_table_remove(table, *key);
        }
    }
}

void
pcmk__add_alert_key_int(GHashTable *table, enum pcmk__alert_keys_e name,
                        int value)
{
    for (const char **key = pcmk__alert_keys[name]; *key; key++) {
        crm_trace("Inserting alert key %s = %d", *key, value);
        g_hash_table_insert(table, strdup(*key), pcmk__itoa(value));
    }
}