Ad Widget

Collapse

Zabbix API maintenance creation - Host not in maintenance mode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • y4nn1ck
    Junior Member
    • Aug 2020
    • 22

    #1

    Zabbix API maintenance creation - Host not in maintenance mode

    Hello everyone,
    I've been working on some automation on a tool i built myself and i'm running into a little issue. I use a webpage to create maintenance which in turns create the VM snapshot and the Zabbix maintenance. It all works but the host in Zabbix is not getting into maintenance mode.


    My php page sends information to a python script which is making the API call. Here's that portion of script that is creating the maintenance :

    Code:
    m = requests.post(ZABBIX_API_URL,json={
                        "jsonrpc": "2.0",
                        "method": "maintenance.create",
                        "params": {
                            "name": maintenance_name,
                            "active_since": int(active_since),
                            "active_till": active_till,
                            "tags_evaltype": 0,
                            "hostids": [f"{hostid}"],
                            "timeperiods": [
                                {
                                    "start_time": int(active_since),
                                    "period": period
                                }
                            ]
                        },
                        "auth": AUTHTOKEN,
                        "id": 1
                    })​
    When i look into Zabbix's database, my host is not update with the maintenance id and the tag ::

    Code:
    MariaDB [zabbixdb]> select * from hosts WHERE hostid=11856 \G
    *************************** 1. row ***************************
                hostid: 11856
          proxy_hostid: NULL
                  host: CNQ-Linux2204
                status: 0
         disable_until: 0
                 error:
             available: 0
           errors_from: 0
            lastaccess: 0
         ipmi_authtype: -1
        ipmi_privilege: 2
         ipmi_username:
         ipmi_password:
    ipmi_disable_until: 0
        ipmi_available: 0
    snmp_disable_until: 0
        snmp_available: 0
    maintenanceid: NULL
    maintenance_status: 0
      maintenance_type: 0
      maintenance_from: 0
    ​
    I tried updating the host through an SQL command, which works but when the maintenance is over, the host does not get updated back to the default value.

    I'm under the impression that there's some underlining process that manages the host status for maintenance.

    Any ideas ?
  • tim.mooney
    Senior Member
    • Dec 2012
    • 1427

    #2
    Always include what version of Zabbix you're using. It matters.

    Here's the Perl 5 code my site uses to create a one-time maintenance via the API, for Zabbix 5.0.x:

    Code:
        my $json_req = {
            'jsonrpc'   => '2.0',
            'method'    => 'maintenance.create',
            'params'    => {
                'name'              => $name,
                'active_since'      => $start_time,
                'active_till'       => $end_time,
                'maintenance_type'  => 0,
                'timeperiods'       => [
                    {
                        'timeperiod_type'   => 0,
                        'start_date'        => $start_time,
                        'period'            => $end_time - $start_time
                    }
                ]
            },
            'auth'      => $auth_tok,
            'id'        => $json_request_id++
        };
    
    
        #
        # either $group_ref or $host_ref (or both) should contain some
        # ids.
        #
        if (!@{$group_ref} and !@{$host_ref}) {
            carp "No hostgroups or hosts specified for maintenance period\n";
            return undef();
        }
    
        if (@{$group_ref} and $#$group_ref >= 0) {
            $json_req->{'params'}->{'groupids'} = $group_ref;
        }
    
        if (@{$host_ref} and $#$host_ref >= 0) {
            $json_req->{'params'}->{'hostids'} = $host_ref;
        }
    
        #
        # done building the JSON structure, now call maintenance.create
        #
        my $json_response = $rpc_client->call($url, $json_req);

    Comment

    • y4nn1ck
      Junior Member
      • Aug 2020
      • 22

      #3
      Always include what version of Zabbix you're using. It matters.
      Sorry, i am using Zabbix v5.0.3

      I tried adding some of the parameter you had that i did not.. But my host is still not "under maintenance".

      Here's the summary of the maintenance created by my script :

      Click image for larger version

Name:	image.png
Views:	660
Size:	56.5 KB
ID:	457346

      Everything looks pretty good... If i make one manually, it will look exactly the same on the frontend but the host does get updated with the maintenance information.

      Comment

      • tim.mooney
        Senior Member
        • Dec 2012
        • 1427

        #4
        As you've said, the maintenance you've created via the API looks fine.

        My next suggestion would be to consider updating to the latest release in the 5.0 series, currently 5.0.30. There have been many, many fixes since 5.0.3 was released, including a few fixes involving the API. I don't see anything in the release notes that specifically mentions your issue, but it still may be worth the upgrade.

        Comment

        • y4nn1ck
          Junior Member
          • Aug 2020
          • 22

          #5
          I'm already at that specific version..
          Click image for larger version

Name:	image.png
Views:	637
Size:	2.3 KB
ID:	457377

          Comment


          • tim.mooney
            tim.mooney commented
            Editing a comment
            You said you were running version 5.0.3, above, not 5.0.30.
        • y4nn1ck
          Junior Member
          • Aug 2020
          • 22

          #6
          Sorry my bad

          Comment

          • y4nn1ck
            Junior Member
            • Aug 2020
            • 22

            #7
            To anyone that is interested, i finally got back to it and found a fix. The problem was how i called the "timeperiods"... Here's what worked :
            PHP Code:
             "timeperiods": [
                                        {
                                            
            "timeperiod_type" 0,
                                            
            "start_time"0,
                                            
            "day"1,
                                            
            "period"period
                                        
            }​ 
            Now it created the maintenance and it is able to close it after the time period... kinda odd when you look at the documentation, it says not to use the "day" parameter. Oh well... i'm happy it works.

            Comment

            Working...