使用者工具

網站工具


linux:monitor:zabbix_alert

利用google api calendar 發送SMS

因為發送訊息,是以python程式寫成。運用Google API Calendar等工具將信息傳到Google日曆,再利用Google日曆提供手機簡訊提醒工具來達成SMS的目的。

安裝

安裝python必要套件。 (easy_install )

#yum install python-setuptools
#easy_install python-gflags
#easy_install httplib2
#easy_install argparse
#easy_install rfc3339
#easy_install google-api-python-client

申請使用Google API金鑰

因為是使用Google API,需要到 https://code.google.com/apis/console/ (使用google帳號登入)申請API金鑰。

  1. 先到 service 開啟 calendar api (舊版控制台)
  2. 再到 API Access, 就可以找到程式所需的資訊(舊版控制台)。分別是Client ID: Client secret: API key:等項目
  3. 用python撰寫 發送訊息到API的程式,利用上面查到Client ID: Client secret: API key分別填入以下程式內 gapi_calendar.py
    #!/usr/bin/python
    # -*- coding: utf8 -*-
    import gflags
    import httplib2
    import sys
    import time
    from apiclient.discovery import build
    from oauth2client.file import Storage
    from oauth2client.client import OAuth2WebServerFlow
    from oauth2client.tools import run
    from rfc3339 import rfc3339
    FLAGS = gflags.FLAGS
    
    FLOW = OAuth2WebServerFlow(
    client_id='Your Client ID',
        client_secret='Your Client secret',
        scope='https://www.googleapis.com/auth/calendar',
        user_agent='APIProject/0.1')
    
    # To disable the local server feature, uncomment the following line:
    gflags.FLAGS.auth_local_webserver = False
    
    # If the Credentials don't exist or are invalid, run through the native client
    # flow. The Storage object will ensure that if successful the good
    # Credentials will get written back to a file.
    
    storage = Storage('/var/lib/zabbixsrv/alertscripts/calendar.dat') ==>(網址進行授權完成後,會在此路徑下產生calendar.dat)
    credentials = storage.get()
    if credentials is None or credentials.invalid == True:
      credentials = run(FLOW, storage)
    
    # Create an httplib2.Http object to handle our HTTP requests and authorize it
    # with our good Credentials.
    http = httplib2.Http()
    http = credentials.authorize(http)
    
    # Build a service object for interacting with the API. Visit
    # the Google APIs Console
    # to get a developerKey for your own application.
    service = build(serviceName='calendar', version='v3', http=http,
           developerKey='Your API key')
    
    #zb_sendto = 日曆ID ,zb_title = 主旨 , zb_body= 說明
    zb_sendto= sys.argv[1]
    zb_title= sys.argv[2]
    zb_body= sys.argv[3]
    sttime = time.time()
    endtime = time.time()+60*60
    event = {
      'summary': zb_title,
      'location': 'QY_office',
      'description': zb_body,
      'start': {
       'dateTime': rfc3339(sttime),
      'timeZone': 'Asia/Taipei'
      },
      'end': {
        'dateTime': rfc3339(endtime),
        'timeZone': 'Asia/Taipei'
    },
      "reminders": {
        "useDefault": False,
        "overrides": [
          {
            "method": "email",
            "minutes": 0
          },
    {
            "method": "popup",
            "minutes": 0
          },
    {
            "method": "sms",
            "minutes": 0
          },
        ]
      },
    }
    
    created_event = service.events().insert(calendarId=zb_sendto, body=event).execute()
    print created_event['id']
  4. 執行此程式 參數1:日曆ID(google mail ) 2.標題 3.說明)
    #python gapi_calendar.py [email protected] TilteTest88 contextTest88
    • 使用此程式產生的網址進行授權 要求是否允許 使用google API 發送訊息到日曆ID
    • 貼上授權碼後完成授權 , 會在目錄下產 calendar.dat
    • 送出events 後會print event id ,就完成了。

參考資料

linux/monitor/zabbix_alert.txt · 上一次變更: 2014/03/23 18:00 由 ali88