Ad Widget

Collapse

API how to create trigger

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chemistmail
    Junior Member
    • Sep 2010
    • 3

    #1

    API how to create trigger

    Hello all.
    Sorry for my bad english.
    I want to create trigger with API.
    But how?
    I have installed zabcon.
    this code create item
    PHP Code:
    #!/usr/bin/ruby      
    require './zabbixapi.rb' 

    zbx=ZbxAPI.new(url)
    zbx.login(login,password)
    name "test"
    hostid 10161
    item_p 
    = { 
       
    'description'=>name
       
    'key_'=>"my.key",
       
    'hostid'=>hostid,
       
    'type'=>'0'
       'data_type'
    =>0,                                                                                                         
       
    'value_type'=>0,                                                                                                        
       
    'units'=>"%"  }  

    uid zabbix.item.create(item_p
    But how to create trigger.
  • chemistmail
    Junior Member
    • Sep 2010
    • 3

    #2
    )) thanks

    PHP Code:
        expression="{#{hostname}:vfs.fs.size[#{disk},pfree].last(0)}<10"
        
    description="API Free diskspace on {HOSTNAME} volume #{disk}"
        
    item_t={
              
    'hostid' => hostid,
              
    'expression'=>expression,
              
    'description'=>description,
              
    'priority'=>HIGH,
              
    'status'=>ENABLE,
        }
    #    p item_t
        
    begin
         uid 
    zabbix.trigger.create(item_t)
          
    "cant create trigger on #{hostname} disk #{disk}" if uid.nil?
        
    end 

    Comment

    • xuxf
      Junior Member
      • May 2010
      • 4

      #3
      let me try,

      Comment

      • nelsonab
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Sep 2006
        • 1233

        #4
        looks like you're on the right track. If you have any issues let me know.
        RHCE, author of zbxapi
        Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
        Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

        Comment

        • chemistmail
          Junior Member
          • Sep 2010
          • 3

          #5
          create trigger

          in zabbixapi.rb comment 2 string
          PHP Code:
              if !resp["error"].nil?
          ##      raise ZbxAPI_GeneralError, resp["error"] # comment it!
              
          end
               
                          
          return resp
          ##             rescue Redirect                  # comment it!
                          
          redirects+=1
                               retry 
          if redirects<=5
                               raise ZbxAPI_GeneralError
          "Too many redirects"
                       
          end 


          and now
          here script for create check drive on servers

          PHP Code:
          #!/usr/bin/ruby

          require './zabbixapi.rb'
           
          zbx=ZbxAPI.new('http://zabbix.server.com')
          zbx.login('login','password')

          # trigger
          HIGH=4
          AVERAGE
          =3
          WARNING
          =2
          INFORMATION
          =1

          ENABLE
          =0
          DISABLE
          =1

          # return hostid
          def hostid_from_hostname(hostname,zabbix)
           for 
          host in zabbix.host.get({"extendoutput"=>true,'pattern'=>"#{hostname}"})
               return 
          host['hostid']
           
          end
          end

          def add_disk_check
          (hostname,zabbix)
           
          # get drive from servers
           
          ssh=`ssh -o "StrictHostKeyChecking no" -q #{hostname} 'df -l -P -h | tail -n +2' `

           
          hostid=hostid_from_hostname(hostname,zabbix)
           for 
          l in ssh
              disk
          =l.split[5].gsub("/","\/")
              
          name="API Free diskspace on #{disk}"

              
          item_p = {
               
          'description'=>name,
               
          'key_'=>"vfs.fs.size[#{disk},pfree]",
               
          'hostid'=>hostid,
               
          'type'=>'0',
               
          'data_type'=>0,
               
          'value_type'=>0,
               
          'units'=>"%"
              
          }

              
          begin
               uid 
          zabbix.item.create(item_p)
               
          "cant create item on #{hostname} disk #{disk}" if uid.nil?
              
          end

              expression
          ="{#{hostname}:vfs.fs.size[#{disk},pfree].last(0)}<10"
              
          description="API Free diskspace on {HOSTNAME} volume #{disk}"

              
          item_t={
                   
          'hostid' => hostid,
                   
          'expression'=>expression,
                   
          'description'=>description,
                   
          'priority'=>HIGH,
                   
          'status'=>ENABLE,
              }

              
          begin
               uid 
          zabbix.trigger.create(item_t)
               
          "cant create trigger on #{hostname} disk #{disk}" if uid.nil?
              
          end

           end
          end

          # now create items and triggers on all servers
          for host in zbx.host.get({"extendoutput"=>true})
           
          id=host['hostid']
           
          hostname=host['host']
           
          puts "#{id} -- #{hostname}"
           
          add_disk_check(hostname,zbx)
          end

          # the end 
          its work.

          I have scripts for create items and triggers for loadaverage and other.
          Now i write script for create items, triggers and graphs for checking openvz user_beancounters.
          I can write it in wiki, but prefer russian langvich. )

          English is not my nature langvich.
          По русски было бы проще.
          Last edited by chemistmail; 21-09-2010, 22:34.

          Comment

          Working...