Load host information from cmk modules or hosts.mk instead of using webapi in plugin?

I’m writing an service icon plugin, where I’m sending service status to an external REST endpoint.
To identify the host in the external system, you need an asset id and not just the hostname. I’ve stored the asset id as the host alias. But of cause you only get the hostname in the row data when running the plugin.
The question is, can i load the host info using the hosts.mk file somehow, instead of having to use the webapi or is there any way through the cmk modules to fetch host information?
I have tried to load the hosts.mk file through cmk.store.load_from_mk_file() / cmk.store.load_mk_file() but these throws exceptions due to the FOLDER_PATH variable.
I’we been through a lot of the python modules to see if there is anything to return host information, but can’t seem to find anything, so i’m a bit stuck…

But why not use the api or lql ? I would go for one of them. Even though I can understand your approach.

It’s just a bit… dumb, because you need to do an external call to cmk from cmk and do unnecessary load, for something that’s inside the system.
But i think I’m going to go the API way for now and see if anyone comes up with an alternative. :slight_smile:

1 Like

If someone more into it knows how to it would be awesome, ill keep a track on this one

I figured it out
You can use the watolib to load the host information

import watolib
watolib.Host.host(hostname)
This will return all the host information directly from the host file:
{’_cached_host_tags’: None, ‘_name’: ‘mighty.host’, ‘_folder’: Folder(‘new_hosts’, u’new_hosts’), ‘_effective_attributes’: None, ‘_cluster_nodes’: None, ‘_attributes’: {‘alias’: u’mymightyhost’}}

2 Likes

@sbp
where do you save your code e.g. path? I want also use the Check_MK code/framework in PyCharm, but I don’t know how… can you say where do you put your code ?

Thanks

WelI I’m sitting in windows and have my dev server on a remote machine, so i mapped at drive to the install folder, so i can edit the files directly in local/share/check_mk/*, i haven’t found any modules or a way to use the installed interpreter this way. I dunno if you maybe could create a virt env. directly on the linux box and have access to the cmk modules. But i think i will try to look into that :slight_smile:

1 Like

Thanks for your answer @sbp,

On my dev machine I install pycharm and I have access directly to the local/share/check_mk

My goal it’s to use those modules which check_mk use internal…

Do you now:

I like to ask the question of @tavanez to you @sbp again. Why you aren’t using the original preexisting access points checkmk offers you?
I can understand your point of performance for using the web api if you run on the same system. But the livestatus (lql) communicates with the core directly and has no significant performance issues also on high frequent queries. This fuction is also used in multisite environments for the sites to communicate with each other. With using this method you also get all informations you need and have also the posibility to get the export python formated (or some others).
Using the intern checkmk function will give you some problem in the future if checkmk is ported to python 2.7 or changes internal functions.
For access to checkmk informations the api or lql are the right way, that’s my opinion.

1 Like

That is a good point :slight_smile:
I think it was mostly the case of running a plugin, that had to call that way around, and i have cases where a host have 100+ services, and having to generate 100+ icons where i have to load these info for each one, seems like a lot of load for something you could get more directly, but i get the point of having the risk of changes in versions or methods.

I am running a information system on the lql which could run over each host in our system (around 2.5k) to get seperate informations and I never had noticed any performance issues on that or long runtimes.

2 Likes

@tosch can you please post one example how we can use the lql ? I never use it before… thanks

Documentation for it here:https://checkmk.com/cms_livestatus.html and here: https://checkmk.com/cms_livestatus_references.html

2 Likes

A simple snippets to find all servers, not clients and not network devices, no printers and so on in our system. host_custom_variables can be used to filter tags within livestatus and the regex before windows is used to eliminate the catch of directories containing windows, because the directories are also deliviered via host_custom_variables.

This snippet is used inside a simple shell script which can be accessed as service via xinetd.

Just play with it at the console to see, what is possible and how fast you get the informations, also to a lot of hosts.

cat << EOF | unixcat /opt/omd/sites/<site>/tmp/run/live
GET hosts
Stats: host_name !=
Filter: host_custom_variables ~~ TAGS sles
Filter: host_custom_variables ~~ TAGS os400
Filter: host_custom_variables ~~ TAGS [^/]windows
Filter: host_custom_variables ~~ TAGS intern
Or: 4
EOF
1 Like

Thank you so much @tosch & @sbp