Ad Widget

Collapse

zabbix-agent2 and systemd Monitoring

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slips121
    Junior Member
    • Aug 2024
    • 2

    #1

    zabbix-agent2 and systemd Monitoring

    I have the following preprocessing Javascript rule for a custom template I'm creating to monitor systemd services on Linux:

    Code:
    my_dict = JSON.parse(value);
    var output_dict = [];
    
    for (var index in my_dict) {
    // Only pull keys you want to modify
    service_name = my_dict[index]['{#UNIT.NAME}'];
    active_state = my_dict[index]['{#UNIT.ACTIVESTATE}'];
    sub_state = my_dict[index]['{#UNIT.SUBSTATE}'];
    
    // Remove .service from systemd name (looks ugly as an Item name in Zabbix).
    my_dict[index]['{#UNIT.NAME}'] = service_name.replace('.service', '');
    
    // Modify the value of ActiveState to include the substate of a systemd service.
    my_dict[index]['{#UNIT.ACTIVESTATE}'] = active_state + " (" + sub_state + ")";
    }
    
    return JSON.stringify(my_dict);
    The resulting JSON data is as expected, where UNIT.ACTIVESTATE is now a combination of ActiveState and SubState:
    Code:
    {
    "{#UNIT.NAME}": "loadmodules",
    "{#UNIT.DESCRIPTION}": "Load legacy module configuration",
    "{#UNIT.LOADSTATE}": "loaded",
    "{#UNIT.ACTIVESTATE}": "inactive (dead)",
    "{#UNIT.SUBSTATE}": "dead",
    "{#UNIT.FOLLOWED}": "",
    "{#UNIT.PATH}": "/org/freedesktop/systemd1/unit/loadmodules_2eservice",
    "{#UNIT.JOBID}": 0,
    "{#UNIT.JOBTYPE}": "",
    "{#UNIT.JOBPATH}": "/",
    "{#UNIT.UNITFILESTATE}": "enabled"
    }​
    However, in my Item prototype, when I poll for systemd.unit.info["{#UNIT.NAME}.service"] it still returns just ActiveState and not the combination that I preprocessed for. It was my understanding that Items would take from the data pulled from Discovery, it seems like this key is just doing another separate poll. Any ideas on how to get the data I already polled for from Discovery?
  • cyber
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Dec 2006
    • 4807

    #2
    If you want data from discovery data, you need to make that item as dependent item and extract that info from "original data". If you just create a new item using a specific agent item key, of course it will go and poll it again.
    But in that case you also need to run that "discovery" constantly to obtain new data.

    Comment

    • slips121
      Junior Member
      • Aug 2024
      • 2

      #3
      I changed the Item's key to systemd.unit.get["{#UNIT.NAME}.service"] and then added this for the preprocessing:

      Code:
      if (typeof value === 'string') {
      try {
      value = JSON.parse(value);
      } catch (e) {
      return "Error parsing JSON: " + e.message;
      }
      }
      
      if (typeof value === 'object' && value !== null) {
      active_state = value['ActiveState']['text'];
      sub_state = value['SubState'];
      if (sub_state === undefined) {
      return "SubState is undefined";
      } else if (sub_state === null) {
      return "SubState is null";
      } else {
      return active_state + " (" + sub_state + ")";
      }
      } else {
      return "Value is not an object";
      }

      Comment

      Working...