Ad Widget

Collapse

Scripted maintenance period does not properly show in maintenance list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • waardd
    Member
    • Aug 2014
    • 82

    #1

    Scripted maintenance period does not properly show in maintenance list

    When we create a maintenance period with a script (see further down for the script) the periods are not showing in the right way in the list.
    See picture below, it shows several pages but all are empty

    Click image for larger version

Name:	Maintenance_showing_odd.PNG
Views:	272
Size:	20.7 KB
ID:	388960
    I am wondering if its a bug in Zabbix where we for instance use characters that are forbidden or something.... or is it the json calls in the script?

    The script we use is as following:
    Code:
    #!/bin/bash
    
    ###################################################################
    #Script Name    : zabbix_json_tst.sh
    #Description    : Set the maintenance period for a zabbix host-group
    #Author         : Danny de Waard
    #Email          : [email protected]
    ###################################################################
    
    # functions
    function_help () {
            echo -e "The parameters for this tool are:\n"
            echo "-u or --user      username for zabbix"
            echo "-p or --password  password for the zabbix user"
            echo "-g or --group     zabbix hostgroup to set maintenance for"
            echo "-n or --name      Name for the maintenance period (no spaces allowed or quoted)"
            echo "-s or --start     starttime-date in quoted yyyy-mm-dd hh:mm:ss (default is starttime/date of script)"
            echo "-d or --duration  duration of maintenance period in seconds"
            echo "                  300 seconds is 5 minutes"
            echo "                  1800 seconds is 30 minutes"
            echo "                  3600 seconds is 1 hour"
            echo "                  default is 3600 seconds"
    }
    
    function_GetAutorization () {
    #Requeststring for Authorization key
    ReqAuth='{"jsonrpc": "2.0",
                    "method": "user.login",
                    "params": {
                            "password": "'"$passwd"'",
                            "user": "'"$usr"'"
                    },
                    "id": 0
            }'
    
    # get authorization string
    result=`curl -i -H "Content-Type:application/json-rpc" -X POST -d "$ReqAuth" "$url"`
    
    Auth=`echo $result | awk -F"\"" '{print $8}'`
    
    echo "The autorization string is: $Auth"
    }
    
    function_GetGroupId () {
    #Requeststring for hostgroupid
    ReqGrId='{"jsonrpc": "2.0",
                    "method": "hostgroup.get",
                    "params":{
                            "filter":{
                                    "name":["'"$hgroup"'"]
                            }
                    },
                    "auth": "'"$Auth"'",
                    "id": 1
            }'
    
    # get groupid string
    result=`curl -i -H "Content-Type:application/json-rpc" -X POST -d "$ReqGrId" "$url"`
    
    GroupId=`echo $result  | awk -F"\"" '{print $10}'`
    
    echo "GroupId for ${hgroup} is ${GroupId}."
    }
    
    function_SetMaintenance () {
    ReqSetMaint='{"jsonrpc": "2.0",
                    "method":"maintenance.create",
                    "params": {
                            "name":"'"$maintname"' - '"$start"'",
                            "active_since":"'"$start"'",
                            "active_till":"'"$end"'",
                            "groupids":[
                                    "'"$GroupId"'"
                            ],
                            "timeperiods": [
                                    {
                                            "timeperiod_type": 0,
                                            "start_date": "'"$start"'",
                                            "period": '"$duration"'
                                    }
                                    ]
                            },
                            "auth":"'"$Auth"'",
                            "id":1}'
    
    result=`curl -i -H "Content-Type:application/json-rpc" -X POST -d "$ReqSetMaint" "$url"`
    
    MaintId=`echo $result | awk -F"\"" '{print $10}'`
    
    # Error check
    re='^[0-9]+$'
    if ! [[ $MaintId =~ $re ]] ; then
            echo -e "\nWatch it!\n"
            echo -e "Setting the maintenance period has failed. Check the parameters\n"
            function_help
            exit 1
    fi
    
    echo "MaintenanceId $MaintId is set."
    }
    
    # default values
    # maintname="TestMaintenance"
    start=`date +%s`
    end=$((start+duration))
    duration=3600
    url="http://lsrv2289.linux.rabobank.nl/zabbix/api_jsonrpc.php"
    
    # processing the parameters
    
    # Check if there are any paramaters
    if [ $# -eq 0 ]
      then
      echo -e "No arguments supplied\n"
      function_help
      exit
    fi
    
    while [ "$1" != "" ]; do
            case $1 in
                    -u |--user )            shift
                                            usr=$1
                                            ;;
                    -p | --password )       shift
                                            passwd=$1
                                            ;;
                    -g | --group )          shift
                                            hgroup=$1
                                            ;;
                    -n | --name )           shift
                                            maintname=$1
                                            ;;
                    -d | --duration )       shift
                                            duration=$1
                                            ;;
                    -s | --start )          shift
                                            start=`date -d "${1}" +%s`
                                            ;;
                    -h | --help )           function_help
                                            exit
                                            ;;
                    * )                     echo "You really got no clue..."
            esac
            shift
    done
    
    # Setting end time
    end=$((start+duration))
    
    
    # setting the maintenance period
    function_GetAutorization
    function_GetGroupId
    function_SetMaintenance
Working...