Icingaweb2 module creation problems

I am currently creating a module to display special matadata from individual hosts. Basically, this also works with fake data. I skiped this because I was not able to get the daten. Most of it is JS for the frontend. To my luck I am missing a list of hosts that have a specific host.var set. But I seem to be too stupid for PHP and can’t get a host site from either the Monitring module or the Icinga\Data\Db module. Unfortunately I have not managed to debug this either.

Does anyone have a suggestion for an IndexController or a separate Xcontroller that I can use to return this data to the view?
Gladly also with filter or build the SQL myelf.

icinga2 2.14.2
icinga2-ido-pgsql 2.14.2
icingaweb2 2.11.4
icingaweb2-module-monitoring 2.11.4

Greetings Marco

you can upload the existing module to github that would help to understand what you are trying to achieve

Hi,
thanks for your fast response. Upload is not needed.

I like to have a application controller like indexController with:

namespace  Icinga\Module\myname\Controllers;

use Icinga\Web\Controller;
use this;
use that;

class IndexController extends Controller
{
    public function indexAction()
    {
         $hosts=(QueryHosts from a Backend like IDO or MonitoringModule)
         $this->view->hosts = $hosts;
    }
}

And a view like index.phtml

<script>
    var hosts = <?php echo json_encode($this->hosts); ?>;
</script>

The JS in den modue.js will do the rest.

Regards Marco

here is the code for the monitoring module:

and here for the icingadb-module:

I’m happy to help more but only if this is for a broader use and published.

Best Regards
Nicolas

Thanks for your time. At the moment I couldn’t share it, and I don’t know if it would be possible later. I understand you and will try to see if I can make progress myself. The module might grow into a version that someone else can use.

Regards, Marco

If you want query your onw data from icinagdb, you can use this example.

<?php
namespace Icinga\Module\MyModule\Controllers;

use Icinga\Web\Controller;
use Icinga\Data\Db\DbConnection;

class IndexController extends Controller
{
    public function indexAction()
    {

        $db = DbConnection::fromResourceName('icingadb');

        $query = $db->select()
            ->from(['h' => 'host'], ['host_name' => 'name', 'host_display_name' => 'display_name',  ])
            ->join(array('hv' => 'host_customvar'), 'h.id = hv.host_id', array('host_id' => 'hv.host_id' ))
            ->join(array('v' => 'customvar'), 'hv.customvar_id = v.id', array('customvar_name' => 'name', 'customvar_value' => 'value'))
            ->join(array('hs' => 'host_state'), 'hs.host_id = h.id', array('hard_state' => 'hard_state'))
            ->where('v.name', 'mycustomvar' )
            ->order('h.name ASC');
        $results = $db->fetchAll($query); 
        echo $results;  // or do some other fancy stuff

    }
}

This is only an example and returns only one specific custom var of any host when the custom var is set. But this is my usecase… As an alternative you can use hasMany or belongsTo to define relations in the controller, but this is not implemented in my code yet.

Like this…

$this->belongsTo(Host::class, 'host_id', 'object_id');

My JS can now access the data which is returned by the controller

I hope this helps someone else…

some things to consider:

  • your resource name does not need to be icingadb
  • this does not get the most current data since most current data is stored in redis
    • host state is fine but any check output is most likely outdated