Uploaded image for project: 'Observium'
  1. Observium
  2. OBS-2516

Passing environment variables to custom sms alert script

Details

    • Help
    • Resolution: Unresolved
    • Trivial
    • None
    • None
    • Alerting
    • None

    Description

      Hello,

      I write custom sms alert script.

      In alerts, I point to file location:

      External program path      /opt/observium/alarmy/sms.py

       

      My question is:

      how to pass environment variables correctly to this script?:

      http://docs.observium.org/alerting_transports/#script

      this is only test, so please don't judge this:

      cat sms.py
      #!/usr/bin/python
      import os

      f = open('output.txt', 'w')
      #f.write(os.environ['OBSERVIUM_ALERT_STATE'])
      f.write(os.environ['OBSERVIUM_ALERT_ID'])

      #f.write(OBSERVIUM_ALERT_STATE)

       

      ??

      output.txt is empty now.

      Maybe variables must be used from others in special set (not only one variable).

      I'm not sure this question is asked in right place, but if someone can help me, I will appreciate any advice.

       

      Attachments

        Activity

          [OBS-2516] Passing environment variables to custom sms alert script

          Hello,

          thank You for your answer.

          I improve a little my testing code.

          My script had a little error.

          Path to file should be unrelative:

           

          f = open("/opt/observium/alarmy/test", 'w')
          

          My testing program code is now:

           

          cat sms.py 
          #!/usr/bin/python
          import os
           
          print(os.environ)
          f = open("/opt/observium/alarmy/test", 'w')
           
           
          f.write("'OBSERVIUM_ENTITY_ID':\t\t")
          f.write(os.environ['OBSERVIUM_ENTITY_ID'])
          f.write('\n')
           
          f.write("'OBSERVIUM_DEVICE_UPTIME':\t")
          f.write(os.environ['OBSERVIUM_DEVICE_UPTIME'])
          f.write('\n')
           
          f.write("'OBSERVIUM_ENTITY_TYPE':\t")
          f.write(os.environ['OBSERVIUM_ENTITY_TYPE'])
          f.write('\n')
           
          f.write("'OBSERVIUM_DURATION':\t\t")
          f.write(os.environ['OBSERVIUM_DURATION'])
          f.write('\n')
           
          f.write("'OBSERVIUM_DEVICE_ID':\t\t")
          f.write(os.environ['OBSERVIUM_DEVICE_ID'])
          f.write('\n')
          #.... and so on

           

           

          Location script is:

           

          /opt/observium/alarmy/sms.py
          

          cd /opt/observium/alarmy
          

          I run test script:

          ../test_alert.php -a 2 -d
          

          Output: (print os.environ):

          {
          'OBSERVIUM_ENTITY_ID': '2',
          'OBSERVIUM_DEVICE_UPTIME': '24 days, 17h 37m 36s',
          'OBSERVIUM_ENTITY_TYPE': 'port',
          'OBSERVIUM_DURATION': '1m 31s (2017-11-29 11:15:06)',
          'LOGNAME': 'root',
          'USER': 'root',
          'OBSERVIUM_DEVICE_ID': '1',
          'HOME': '/root',
          'PATH': '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin',
          'OBSERVIUM_ENTITY_GRAPHS_ARRAY': 'Array',
          'OBSERVIUM_METRICS': 'ifInOctets_perc = 0\n             ifOutOctets_perc = 0',
          'LANG': 'pl_PL.UTF-8',
          'TERM': 'xterm-256color',
          'SHELL': '/bin/bash',
          'SHLVL': '2',
          'OBSERVIUM_DEVICE_HARDWARE': 'Smart-UPS RT 5000 RM XL X438  451.3X438.W',
          'OBSERVIUM_TITLE': 'RECOVER: [172.16.15.10] [port] [lance] test',
          'HISTSIZE': '1000',
          'OBSERVIUM_ENTITY_NAME': 'lance',
          'OBSERVIUM_ENTITY_LINK': '<a href="http://observium-test/device/device=1/tab=port/port=2/" class="entity-popup " data-eid="2" data-etype="port">lance</a>',
          'OBSERVIUM_ALERT_TIMESTAMP': '2017-11-29 07:36:18',
          'OBSERVIUM_DEVICE_HOSTNAME': '172.16.15.10',
          'OBSERVIUM_DEVICE_LINK': '<a href="http://observium-test/device/device=1/" class="entity-popup " data-eid="1" data-etype="device">172.16.15.10</a>',
          'OBSERVIUM_ALERT_MESSAGE': 'test',
          'OBSERVIUM_ALERT_URL': 'http://observium-test/device/device=1/tab=alert/alert_entry=2/',
          'OBSERVIUM_ENTITY_DESCRIPTION': '',
          'LC_ALL': 'C',
          'LESSOPEN': '||/usr/bin/lesspipe.sh %s',
          'XDG_SESSION_ID': '117398','_': '/opt/observium/alarmy/sms.py',
          'OBSERVIUM_ALERT_STATE': 'RECOVER',
          'HOSTNAME': 'observium-test',
          'HISTCONTROL': 'ignoredups',
          'PWD': '/opt/observium',
          'OBSERVIUM_CONDITIONS': '',
          'MAIL': '/var/spool/mail/root',
          'OBSERVIUM_ALERT_ID': '2'
          }
          

          cat test:

          'OBSERVIUM_ENTITY_ID':		2
          'OBSERVIUM_DEVICE_UPTIME':	24 days, 17h 37m 36s
          'OBSERVIUM_ENTITY_TYPE':	port
          'OBSERVIUM_DURATION':		1m 31s (2017-11-29 11:15:06)
          'OBSERVIUM_DEVICE_ID':		1
           
          ...
          

          Now actual question.

          How can I get information about another devices:

          172.16.16.10
          172.16.16.11
          172.16.16.20
          172.16.16.21
          172.16.16.22
          172.16.16.23
          .....

           

          And specific port problems within this devices?

           

           

           

          janix Janusz Janik added a comment - Hello, thank You for your answer. I improve a little my testing code. My script had a little error. Path to file should be unrelative:   f = open( "/opt/observium/alarmy/test" , 'w' ) My testing program code is now:   cat sms.py #!/usr/bin/python import os   print(os.environ) f = open( "/opt/observium/alarmy/test" , 'w' )     f.write( "'OBSERVIUM_ENTITY_ID':\t\t" ) f.write(os.environ[ 'OBSERVIUM_ENTITY_ID' ]) f.write( '\n' )   f.write( "'OBSERVIUM_DEVICE_UPTIME':\t" ) f.write(os.environ[ 'OBSERVIUM_DEVICE_UPTIME' ]) f.write( '\n' )   f.write( "'OBSERVIUM_ENTITY_TYPE':\t" ) f.write(os.environ[ 'OBSERVIUM_ENTITY_TYPE' ]) f.write( '\n' )   f.write( "'OBSERVIUM_DURATION':\t\t" ) f.write(os.environ[ 'OBSERVIUM_DURATION' ]) f.write( '\n' )   f.write( "'OBSERVIUM_DEVICE_ID':\t\t" ) f.write(os.environ[ 'OBSERVIUM_DEVICE_ID' ]) f.write( '\n' ) #.... and so on     Location script is:   /opt/observium/alarmy/sms.py cd /opt/observium/alarmy I run test script: ../test_alert.php -a 2 -d Output: (print os.environ): { 'OBSERVIUM_ENTITY_ID' : '2' , 'OBSERVIUM_DEVICE_UPTIME' : '24 days, 17h 37m 36s' , 'OBSERVIUM_ENTITY_TYPE' : 'port' , 'OBSERVIUM_DURATION' : '1m 31s (2017-11-29 11:15:06)' , 'LOGNAME' : 'root' , 'USER' : 'root' , 'OBSERVIUM_DEVICE_ID' : '1' , 'HOME' : '/root' , 'PATH' : '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin' , 'OBSERVIUM_ENTITY_GRAPHS_ARRAY' : 'Array' , 'OBSERVIUM_METRICS' : 'ifInOctets_perc = 0\n             ifOutOctets_perc = 0' , 'LANG' : 'pl_PL.UTF-8' , 'TERM' : 'xterm-256color' , 'SHELL' : '/bin/bash' , 'SHLVL' : '2' , 'OBSERVIUM_DEVICE_HARDWARE' : 'Smart-UPS RT 5000 RM XL X438  451.3X438.W' , 'OBSERVIUM_TITLE' : 'RECOVER: [172.16.15.10] [port] [lance] test' , 'HISTSIZE' : '1000' , 'OBSERVIUM_ENTITY_NAME' : 'lance' , 'OBSERVIUM_ENTITY_LINK' : '<a href="http://observium-test/device/device=1/tab=port/port=2/" class="entity-popup " data-eid="2" data-etype="port">lance</a>' , 'OBSERVIUM_ALERT_TIMESTAMP' : '2017-11-29 07:36:18' , 'OBSERVIUM_DEVICE_HOSTNAME' : '172.16.15.10' , 'OBSERVIUM_DEVICE_LINK' : '<a href="http://observium-test/device/device=1/" class="entity-popup " data-eid="1" data-etype="device">172.16.15.10</a>' , 'OBSERVIUM_ALERT_MESSAGE' : 'test' , 'OBSERVIUM_ALERT_URL' : 'http://observium-test/device/device=1/tab=alert/alert_entry=2/' , 'OBSERVIUM_ENTITY_DESCRIPTION' : '' , 'LC_ALL' : 'C' , 'LESSOPEN' : '||/usr/bin/lesspipe.sh %s' , 'XDG_SESSION_ID' : '117398' , '_' : '/opt/observium/alarmy/sms.py' , 'OBSERVIUM_ALERT_STATE' : 'RECOVER' , 'HOSTNAME' : 'observium-test' , 'HISTCONTROL' : 'ignoredups' , 'PWD' : '/opt/observium' , 'OBSERVIUM_CONDITIONS' : '' , 'MAIL' : '/var/spool/mail/root' , 'OBSERVIUM_ALERT_ID' : '2' } cat test: 'OBSERVIUM_ENTITY_ID' : 2 'OBSERVIUM_DEVICE_UPTIME' : 24 days, 17h 37m 36s 'OBSERVIUM_ENTITY_TYPE' : port 'OBSERVIUM_DURATION' : 1m 31s ( 2017 - 11 - 29 11 : 15 : 06 ) 'OBSERVIUM_DEVICE_ID' : 1   ... Now actual question. How can I get information about another devices: 172.16.16.10 172.16.16.11 172.16.16.20 172.16.16.21 172.16.16.22 172.16.16.23 .....   And specific port problems within this devices?      

          You can try writing the output of the 'env' command to a temporary file to see what environment variables are set.

          adama Adam Armstrong added a comment - You can try writing the output of the 'env' command to a temporary file to see what environment variables are set.

          People

            adama Adam Armstrong
            janix Janusz Janik
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: