Automated bulk import of rules?

Does anyone know a good way for a bulk import of rules?

We still need to migrate several thousand special checks from Nagios to checkmk 2.0.0p1. Many are checks like check_http with unique URLs or check_procs with unique usernames. Many of these checks and their parameters could easily be exported to a csv or Excel file. But I have not yet found a useful way to import this into checkmk rules.

The API only handles complete rulesets for each type of check, which means that you need to get a complete ruleset like check_http, with all individual check_http checks, add one or more new rules to this and then write back the whole ruleset to the API, while hoping your API script didn’t make any mistake that messed up the complete ruleset.

Another approach would be the creation of the raw text configuration to add new rules to a rules.mk file without touching the existing ones. This seems easy enough, but each rule features a unique “id”. And I do not know how this is generated. Any hints?

Has anybody found an efficient and safe way to perform a bulk import of checkmk rules for active/enforced checks?

Kind regards,
Dirk.

But isn’t the API easier to handle than writing the config files by yourself?

In both cases you do not want to make errors. In the API case you get feedback if you make one.

1 Like

Hi,

if the API featured a command add_rule, just like add_host, I’d agree that the API would be easier to handle. But unfortunately you need to first identify the ruleset you want with get_ruleset_info, then retrieve the complete ruleset with get_ruleset, then have your script add the new rules and then use set_ruleset to import this modified ruleset, including all old and new rules.

So, just adding a few text lines to add a Python dictionary to a rules.mk file indeed does seem a lot easier and less risky. There’s probably a good reason, why handling rules is not yet possible with the new REST API, only with the old API. It seems to be a rather complex topic.

Kind regards,
Dirk.

But why? You just write the code that talks to the API and hand it a list of your rules. You do not craft each API call by hand.

Yes, rules are complex. Especially as the ordering is important for the outcome.

1 Like

In the case of rulesets, parameters vary greatly with the type of ruleset. (Ruleset active_checks:http has different parameters from a ruleset like active_checks:ssh, of course). There’s no full documentation on all the possible ruleset parameters, so you’ll need one existing example of each type of rule for reverse-engineering in order to craft a suitable API call.

In the meantime I found out how the rule id’s are generated. Method “create()” of class Rule uses “utils.gen_id”, which uses /proc/sys/kernel/random/uuid to generate the random id’s.

So in my case, I’ll probably choose the approach of generating the text content for rules.mk.

If someone in the forum has already implemented a bulk import of checkmk rules in real-life either by API or by generating rules.mk, I’d be glad, if you could share your experiences!

Kind regards,
Dirk.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.