Create own module - get hosts and it´s associated services as array (php)

This forum was archived to /woltlab and is now in read-only mode.
  • I want to build my own module in Icinga Web2 and I need an array of all hosts and its associated services.

    Does anyone know how to achieve that in a smart way?


    Best regards,

    Tim

  • Your module has to have a dependency to the monitoring module, as this component provides the IDO backend and specific functions to retrieve values from tables there. That's the first thing to know about these things.


    Meaning to say, your e.g. ListController could look like this in application/controllers/ListController.php



    Code is stolen from https://github.com/Icinga/icin…ollers/ListController.php for example, but there are also other modules which do implement such.


    You also need to ensure to apply restrictions to these calls, otherwise your module will show all data. Best is to e.g. copy the one's the monitoring module would apply itself. Though your module can provide its own restrictions.


    In order to simply test the view, add a new html snippet into application/views/scripts/list/index.phtml


    PHP
    1. <div class="content">
    2. <?php
    3. if ($services->hasResult()) {
    4. foreach ($services->peekAhead($compact) as $service) {
    5. echo "Host: " + $service->host_name + " Service: " + $service->service_description + "<br>":
    6. }
    7. }


    Stolen from https://github.com/Icinga/icin…ripts/list/services.phtml in an abstracted way.


    Did not test the above, but that's how gather data from controllers and put them into views from my experience. Still learning a lot myself.


    JoNe provided a link lately here as well: Einige Fragen zu Modulen/Zend-Syntax im IcingaWeb2