<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Arrfab's blog - Foreman</title><link href="https://arrfab.net/" rel="alternate"></link><link href="https://arrfab.net/feeds/foreman.atom.xml" rel="self"></link><id>https://arrfab.net/</id><updated>2017-05-16T00:00:00+02:00</updated><subtitle>Some tips and tricks, mostly around CentOS</subtitle><entry><title>Linking Foreman with Zabbix through MQTT</title><link href="https://arrfab.net/posts/2017/May/16/linking-foreman-with-zabbix-through-mqtt/" rel="alternate"></link><published>2017-05-16T00:00:00+02:00</published><updated>2017-05-16T00:00:00+02:00</updated><author><name>Fabian Arrotin</name></author><id>tag:arrfab.net,2017-05-16:/posts/2017/May/16/linking-foreman-with-zabbix-through-mqtt/</id><summary type="html">&lt;p&gt;It's been a while since I thought about this design, but I finally had time to implement it the proper way, and "just in time" as I needed recently to migrate our &lt;a href="https://www.theforeman.org"&gt;Foreman&lt;/a&gt; instance to another host (from CentOS 6 to CentOS 7)&lt;/p&gt;
&lt;p&gt;Within the CentOS Infra, we use Foreman as an &lt;a href="https://docs.puppet.com/puppet/4.10/nodes_external.html#what-is-an-enc"&gt;ENC&lt;/a&gt; for our Puppet environments (multiple ones). For full automation between configuration management and monitoring, you need some "glue". The idea is that whatever you describe at the configuration management level should be authoritative and so automatically configuring the monitoring solution you have in place in your Infra.&lt;/p&gt;
&lt;p&gt;In our case, that means that we have Foreman/puppet on one side, and &lt;a href="http://www.zabbix.com"&gt;Zabbix&lt;/a&gt; on the other side. Let's see how we can "link" the two sides.&lt;/p&gt;
&lt;p&gt;What I've seen so far is that you use &lt;a href="https://docs.puppet.com/puppet/4.10/lang_exported.html"&gt;exported resources&lt;/a&gt; on each node, store that in another &lt;a href="https://docs.puppet.com/puppetdb/4.4/install_via_module.html"&gt;PuppetDB&lt;/a&gt;, and then on the monitoring node, reapply all those resources. Problem with such solution is that it's "expensive" and when one thinks about it, a little bit strange to export the "knowledge" from Foreman back into another DB, and then let puppet compiles a &lt;em&gt;huge&lt;/em&gt; catalog at the monitoring side, even if nothing …&lt;/p&gt;</summary><content type="html">&lt;p&gt;It's been a while since I thought about this design, but I finally had time to implement it the proper way, and "just in time" as I needed recently to migrate our &lt;a href="https://www.theforeman.org"&gt;Foreman&lt;/a&gt; instance to another host (from CentOS 6 to CentOS 7)&lt;/p&gt;
&lt;p&gt;Within the CentOS Infra, we use Foreman as an &lt;a href="https://docs.puppet.com/puppet/4.10/nodes_external.html#what-is-an-enc"&gt;ENC&lt;/a&gt; for our Puppet environments (multiple ones). For full automation between configuration management and monitoring, you need some "glue". The idea is that whatever you describe at the configuration management level should be authoritative and so automatically configuring the monitoring solution you have in place in your Infra.&lt;/p&gt;
&lt;p&gt;In our case, that means that we have Foreman/puppet on one side, and &lt;a href="http://www.zabbix.com"&gt;Zabbix&lt;/a&gt; on the other side. Let's see how we can "link" the two sides.&lt;/p&gt;
&lt;p&gt;What I've seen so far is that you use &lt;a href="https://docs.puppet.com/puppet/4.10/lang_exported.html"&gt;exported resources&lt;/a&gt; on each node, store that in another &lt;a href="https://docs.puppet.com/puppetdb/4.4/install_via_module.html"&gt;PuppetDB&lt;/a&gt;, and then on the monitoring node, reapply all those resources. Problem with such solution is that it's "expensive" and when one thinks about it, a little bit strange to export the "knowledge" from Foreman back into another DB, and then let puppet compiles a &lt;em&gt;huge&lt;/em&gt; catalog at the monitoring side, even if nothing was changed.&lt;/p&gt;
&lt;p&gt;One issue is also that in our Zabbix setup, we also have some nodes that aren't really managed by Foreman/puppet (but other automation around &lt;a href="http://www.ansible.com"&gt;Ansible&lt;/a&gt;, so I had to use an intermediate step that other tools can also use/abuse for the same reason.&lt;/p&gt;
&lt;p&gt;The other reason also is that I admit that I'm a fan of "event driven" configuration change, so my idea was :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;update a host in Foreman (or groups of hosts, etc)&lt;/li&gt;
&lt;li&gt;publish that change on a secure network through a message queue (so asynchronous so that it doesn't slow down the foreman update operation itself)&lt;/li&gt;
&lt;li&gt;let Zabbix server know that change and apply it (like linking a template to a host)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So the good news is that it can be done really easily with several components :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/theforeman/foreman_hooks"&gt;foreman hooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://mosquitto.org/"&gt;Mosquitto&lt;/a&gt;, a very lightweight MQTT broker/pub/sub client&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/usit-gd/zabbix-cli"&gt;zabbix-cli&lt;/a&gt; , to let us talk to the Zabbix API&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is a small overview of the process :&lt;/p&gt;
&lt;p&gt;&lt;img alt="Foreman MQTT Zabbix" src="/images/mqtt-foreman-zabbix.png" title="Foreman MQTT Zabbix"&gt;&lt;/p&gt;
&lt;h2&gt;Foreman hooks&lt;/h2&gt;
&lt;p&gt;Setting up foreman hooks is really easy: just install the pkg itself (tfm-rubygem-foreman_hooks.noarch), read the &lt;a href="https://github.com/theforeman/foreman_hooks"&gt;Documentation&lt;/a&gt;, and then create your scripts. There are some examples for Bash and python in the &lt;a href="https://github.com/theforeman/foreman_hooks/tree/master/examples"&gt;examples&lt;/a&gt; directory, but basically you just need to place some scripts at specific place[s]. 
In my case I wanted to "trigger" an event in the case of a node update (like adding a puppet class, or variable/paramater change) so I just had to place it under /usr/share/foreman/config/hooks/host/managed/update/.&lt;/p&gt;
&lt;p&gt;One little remark though : if you put a new file, don't forget to restart foreman itself, so that it picks that hooks file, otherwise it would still be ignored and so not ran.&lt;/p&gt;
&lt;h2&gt;Mosquitto&lt;/h2&gt;
&lt;p&gt;Mosquitto itself is available in your favorite rpm repo, so installing it is a breeze. Reason why I selected mosquitto is that it's very lightweight (package size is under 200Kb), it supports TLS and ACL out-of-the box.&lt;/p&gt;
&lt;p&gt;For an introduction to MQTT/Mosquitto, I'd suggest you to read &lt;a href="https://twitter.com/jpmens"&gt;Jan-Piet Mens&lt;/a&gt; dedicated &lt;a href="http://jpmens.net/2013/02/25/lots-of-messages-mqtt-pub-sub-and-the-mosquitto-broker/"&gt;blog post&lt;/a&gt; around it
I even admit that I discovered it by attending one of his talks on the topic, back in the &lt;a href="http://loadays.org/"&gt;Loadays.org&lt;/a&gt; days :-)&lt;/p&gt;
&lt;h2&gt;Zabbix-cli&lt;/h2&gt;
&lt;p&gt;While one can always discuss &lt;a href="https://www.zabbix.com/documentation/3.0/manual/api"&gt;"Raw API"&lt;/a&gt; with Zabbix, I found it useful to use a tool I was already using for various tasks around Zabbix : &lt;a href="https://github.com/usit-gd/zabbix-cli"&gt;zabbix-cli&lt;/a&gt;
For people interested in using it on CentOS 6 or 7, I built the packages and they are on &lt;a href="https://cbs.centos.org/koji/packageinfo?packageID=4477"&gt;CBS&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So I plumbed it in a systemd unit file that subscribe to specific MQTT topic, parse the needed informations (like hostname and zabbix templates to link, unlink, etc) and then it updates that in Zabbix itself (from the log output):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;+&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;20170516&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;43&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;Adding&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;zabbix&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="ss"&gt;&amp;quot;Template CentOS - https SSL Cert Check External&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;host&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="ss"&gt;&amp;quot;dev-registry.lon1.centos.org&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Done&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Templates&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Template&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;CentOS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SSL&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Cert&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;Check&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;External&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;&amp;quot;templateid&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;&amp;quot;10105&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;linked&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;these&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;hosts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lon1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;centos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;&amp;quot;hostid&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;&amp;quot;10174&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Cool, so now I don't have to worry about forgetting to tie a zabbix template to a host , as it's now done automatically. No need to say that the deployment of those tools was of course automated and coming from Puppet/foreman :-)&lt;/p&gt;</content><category term="CentOS"></category><category term="Linux"></category><category term="SysAdmin"></category><category term="Foreman"></category><category term="Zabbix"></category><category term="Mqtt"></category></entry></feed>