Advertisement
Guest User

Untitled

a guest
Apr 28th, 2020
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. from datetime import timedelta, datetime
  2. import os
  3.  
  4. from airflow import DAG
  5. from airflow.operators.dummy_operator import DummyOperator
  6. from airflow.operators.python_operator import PythonOperator
  7.  
  8. # import pyping
  9.  
  10. # Default args for DAG.
  11. default_args = {
  12.     'description': 'Determines whether or not an IP address is active',
  13.     'depends_on_past': False,
  14.     'start_date': datetime(2020, 4, 21),
  15.     'catchup': False,
  16.     'email': ['email@email.com'],
  17.     'email_on_failure': True,
  18.     'email_on_retry': False,
  19.     'retry_delay': timedelta(minutes=5),
  20. }
  21.  
  22. # DAG schedule interval, defined by CRON.
  23. schedule_interval = '0 0 * * *'  # Once a day
  24.  
  25. # Python def for the ping_ip_task.
  26. def ping_ip():
  27.     ip_address = "000.000.000.00.00"  # My laptop IP
  28.     response = os.system("ping -c 1 " + ip_address)
  29.  
  30.     if response == 0:
  31.         pingstatus = "Network Active."
  32.     else:
  33.         pingstatus = "Network Error."
  34.  
  35.     print("\n *** Network status for IP Address=%s is : ***" % ip_address)
  36.     print(pingstatus)
  37.  
  38.     return pingstatus, response
  39.  
  40.  
  41. # Open DAG, give it an ID, schedule, default_args.
  42. with DAG(dag_id='ip_ping', schedule_interval=schedule_interval, default_args=default_args) as dag:
  43.     # Define a Dummy task that does nothing.
  44.     dummy_task = DummyOperator(task_id='dummy_task', retries=3)
  45.  
  46.     # Define a task that returns the network status of an IP Address.
  47.     ping_ip_task = PythonOperator(task_id='ping_ip', python_callable=ping_ip, retries=1)
  48.  
  49.     # Declare order of task execution.
  50.     dummy_task >> ping_ip_task
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement