Ad Widget

Collapse

zabbix api action.create

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TWoF
    Junior Member
    • Nov 2018
    • 5

    #1

    zabbix api action.create

    Good day.
    when accessing zabbix api from powershell, an error is constantly displayed:

    {"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params.","data":"Field "eventsource" is mandatory."},"id":2}

    although "eventsource" is specified:
    Code:
    $params = @{
        body =  @{
            "jsonrpc"= "2.0"
            "method"= "action.create"
            "params"= @{
                "name"="m3"
                "eventsource"=2
                "status"="0"
                "esc_period"=60
                "def_shortdata"="Auto registration: {HOST.HOST}"
                "def_longdata"="Host name: {HOST.HOST}\r\nHost IP: {HOST.IP}\r\nAgent port: {HOST.PORT}"
                    "conditions"=@{"conditiontype"="24"
                        "operator"="2"
                        "value"="m3"}
                    "operations"=@{"esc_step_from"=1
                        "esc_period"=60
                        "optemplate"=@{"templateid"="12103"}
                        "operationtype"=6
                        "esc_step_to"=1}
    
              }      
              "id"= 2
              "auth"= ($auth.Content | ConvertFrom-Json).result} | ConvertTo-Json
    
    uri = "$baseurl/api_jsonrpc.php"
    headers = @{"Content-Type" = "application/json"}
    method = "Post"}
    $result = Invoke-WebRequest @params
    $result.Content
    What am I doing wrong?
  • steeladept
    Member
    • Sep 2018
    • 69

    #2
    Don't play with the API much, yet anyway, but I note that your eventsource is NOT in quotes where all the other values are. Perhaps it is looking for it to be in quotes? Also, I thought key/value pairs were generally separated by : not =, but that may just be my lack of recent powershell/JSON experience. I have to look that up again to be sure of syntax.

    Comment

    • TWoF
      Junior Member
      • Nov 2018
      • 5

      #3
      almost solved. it was necessary to do this:

      Code:
      $params = [pscustomobject]@{
          "jsonrpc" = "2.0";
          "method"="action.create";
          "params"=,@{
              "name"="number";
              "eventsource"=2;
              "def_shortdata"="Auto registration: {HOST.HOST}";
              "def_longdata"="Host name: {HOST.HOST}\r\nHost IP: {HOST.IP}\r\nAgent port: {HOST.PORT}";
                  "conditions"=,@{
                      "evaltype"=0;
                      "conditiontype"=22;
                      "operator"=2;
                      "value"="sdasd"
                  };
                  "operations"=,@{
                      "operationtype"=4;
                      "opgroup"=,@{
                          "groupid"="ids"
                      }
                  }
      };
      "id"= 3;
      "auth"= ($auth.Content | ConvertFrom-Json).result}
      $paramsa =  @{body = ($params | convertTo-Json –Depth 10)
      uri = "$baseurl"
      headers = @{"Content-Type" = "application/json"}
      method = "Post"}
      $action = Invoke-WebRequest @paramsa
      but for some reason it does not create conditions, although I get a response without errors
      What am I doing wrong?

      Comment

      • steeladept
        Member
        • Sep 2018
        • 69

        #4
        First, I want to ask about the action - I don't see you using the uri or method or anything. I am guessing that was a copy/paste typo? If not, that may be the issue. Assuming it is, wouldn't this be an Invoke-RestMethod instead of Invoke-WebRequest? I can't remember when which applies, as they are very similar, perhaps that is the issue?

        Comment

        Working...