Ad Widget

Collapse

How to delete expired maintenance period via API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • natalia
    Senior Member
    • Apr 2013
    • 159

    #1

    How to delete expired maintenance period via API

    Hi,

    Is it any way to delete expired maintenance period via API ?

    Once it expired, maintenance id change to be "0".

    I created zabbix_maintenance.pl script to add/remove host to maintenance period with maintenance name = hostname, but once it expired - I can't add/remove it.

    As a option, it to create with maintenance name =hostname_`date` but again, I will have no way to delete it once it expired

    I have 8000 hosts and a lot of maintenance ....

    Please share your experience how do you manage it

    Thanks
  • galoxucro
    Junior Member
    • May 2018
    • 5

    #2
    In Perl:

    Code:
    #!/usr/bin/perl
    #
    
    $MAXMNTDAYS=$ARGV[0] =~ /^\d+$/ ? $ARGV[0] : 720; # max expired days
    
    use Zabbix::Tiny;
    
    package ZBX;
    
    use Class::Tiny { zabbix => ''};
    
    sub connect {
        my ($self) = @_;
    
        $self->zabbix(Zabbix::Tiny->new(
            server   => $your_api_endpoint,
            password => $your_password,
            user     => $your_username
          )
        );
    } # connect
    
    sub getMaintenances {
        my ($self) = @_;
    
        my @maintenances;
    
        my $maintenances = $self->zabbix->do(
            'maintenance.get',
            {
                output              => [ "maintenanceid", "name", "active_since", "active_till" ],
                sortfield           => [ "name" ],
                limit               => "2048",
            }
        );
    
        for my $maintenance (@$maintenances) {
            push @maintenances, $maintenance;
        }
    
        return @maintenances;
    } # getMaintenances
    
    sub deleteMaintenance {
        my ($self) = @_;
        my $maintenanceid = @_[1];
    
    
        my @maintenanceids = $self->zabbix->do(
            'maintenance.delete',
            [ "$maintenanceid" ]
        );
    
        return @maintenanceids;
    } # deleteMaintenance
    
    1;
    
    #
    # main
    #
    
    my $zbx = ZBX->new();
    
    $zbx->connect();
    
    #
    # check and delete
    #
    
    my @maintenances = $zbx->getMaintenances();
    
    foreach my $mnt (@maintenances) {
        if ($mnt->{active_till} + $MAXMNTDAYS*86400 < time()) {
            $zbx->deleteMaintenance($mnt->{maintenanceid});
    
            my $active_since = localtime($mnt->{active_since});
            my $active_till = localtime($mnt->{active_till});
    
            print( "'$mnt->{name}' deleted from Zabbix -  '$active_since - '$active_till'\n");
        }
    }
    
    exit 0;
    
    
    
    ​
    Last edited by galoxucro; 09-06-2023, 18:19.

    Comment

    Working...