job.go raw

   1  package monitor
   2  
   3  import "time"
   4  
   5  // Job wraps an NS1 /monitoring/jobs resource
   6  type Job struct {
   7  	ID string `json:"id,omitempty"`
   8  
   9  	// The id of the notification list to send notifications to.
  10  	NotifyListID string `json:"notify_list"`
  11  
  12  	// Type of monitor to be run.
  13  	// Available job types:
  14  	//  - http: Do an HTTP request against a webserver
  15  	//  - dns: Do a DNS lookup against a nameserver
  16  	//  - tcp: Connect to a TCP port on a host
  17  	//  - ping: Ping a host using ICMP packets
  18  	Type string `json:"job_type"`
  19  
  20  	// Configuration dictionary(key/vals depend on the jobs' type).
  21  	Config Config `json:"config"`
  22  
  23  	// The current status of the monitor.
  24  	Status map[string]*Status `json:"status,omitempty"`
  25  
  26  	// Rules for determining failure conditions.
  27  	Rules []*Rule `json:"rules"`
  28  
  29  	// List of regions in which to run the monitor.
  30  	// eg, ["dal", "sin", "sjc", "lga", "ams"]
  31  	Regions []string `json:"regions"`
  32  
  33  	// Indicates if the job is active or temporarily disabled.
  34  	Active bool `json:"active"`
  35  
  36  	// Frequency(in seconds), at which to run the monitor.
  37  	Frequency int `json:"frequency"`
  38  
  39  	// The policy for determining the monitor's global status based
  40  	// on the status of the job in all regions.
  41  	// Available policies:
  42  	//  - quorum: Status change when majority status
  43  	//  - all: Status change only when all regions are in agreement
  44  	//  - one: Status change if any region changes
  45  	Policy string `json:"policy"`
  46  
  47  	// Controls behavior of how the job is assigned to monitoring regions.
  48  	// Currently this must be fixed — indicating monitoring regions are explicitly chosen.
  49  	RegionScope string `json:"region_scope"`
  50  
  51  	// Freeform notes to be included in any notifications about this job,
  52  	// e.g., instructions for operators who will receive the notifications.
  53  	Notes string `json:"notes,omitempty"`
  54  
  55  	// A free-form display name for the monitoring job.
  56  	Name string `json:"name"`
  57  
  58  	// Time(in seconds) between repeat notifications of a failed job.
  59  	// Set to 0 to disable repeating notifications.
  60  	NotifyRepeat int `json:"notify_repeat"`
  61  
  62  	// If true, on any apparent state change, the job is quickly re-run after
  63  	// one second to confirm the state change before notification.
  64  	RapidRecheck bool `json:"rapid_recheck"`
  65  
  66  	// Time(in seconds) after a failure to wait before sending a notification.
  67  	NotifyDelay int `json:"notify_delay"`
  68  
  69  	// If true, notifications are sent for any regional failure (and failback if desired),
  70  	// in addition to global state notifications.
  71  	NotifyRegional bool `json:"notify_regional"`
  72  
  73  	// If true the notification will be off
  74  	Mute bool `json:"mute"`
  75  
  76  	// If true, a notification is sent when a job returns to an "up" state.
  77  	NotifyFailback bool `json:"notify_failback"`
  78  }
  79  
  80  // Activate a monitoring job.
  81  func (j *Job) Activate() {
  82  	j.Active = true
  83  
  84  }
  85  
  86  // Deactivate a monitoring job.
  87  func (j *Job) Deactivate() {
  88  	j.Active = false
  89  }
  90  
  91  // Result wraps an element of a JobType's "results" attribute
  92  type Result struct {
  93  	Comparators []string `json:"comparators"`
  94  	Metric      bool     `json:"metric"`
  95  	Validator   string   `json:"validator"`
  96  	ShortDesc   string   `json:"shortdesc"`
  97  	Type        string   `json:"type"`
  98  	Desc        string   `json:"desc"`
  99  }
 100  
 101  // Status wraps an value of a Job's "status" attribute
 102  type Status struct {
 103  	Since  int    `json:"since"`
 104  	Status string `json:"status"`
 105  }
 106  
 107  // StatusLog wraps an NS1 /monitoring/history resource
 108  type StatusLog struct {
 109  	Job    string `json:"job"`
 110  	Region string `json:"region"`
 111  	Status string `json:"status"`
 112  	Since  int    `json:"since"`
 113  	Until  int    `json:"until"`
 114  }
 115  
 116  // Rule wraps an element of a Job's "rules" attribute
 117  type Rule struct {
 118  	Key        string      `json:"key"`
 119  	Value      interface{} `json:"value"`
 120  	Comparison string      `json:"comparison"`
 121  }
 122  
 123  // NewHTTPConfig constructs/returns a job configuration for HTTP type jobs.
 124  // url is the URL to query. (Required)
 125  // method is the HTTP method(valid methods are HEAD, GET, and POST).
 126  // ua is the user agent text in the request header.
 127  // auth is the authorization header to use in request.
 128  // connTimeout is the timeout(in sec) to wait for query output.
 129  func NewHTTPConfig(url, method, ua, auth string, connTimeout int) *Config {
 130  	return &Config{
 131  		"url":             url, // Required
 132  		"method":          method,
 133  		"user_agent":      ua,
 134  		"authorization":   auth,
 135  		"connect_timeout": connTimeout,
 136  	}
 137  
 138  }
 139  
 140  // NewHTTPV3Config constructs/returns a job configuration for HTTP type jobs, with additional V3 fields.
 141  // v3 must be enabled in customer configuration
 142  // url is the URL to query. (Required)
 143  // method is the HTTP method(valid methods are HEAD, GET, and POST).
 144  // ua is the user agent text in the request header.
 145  // auth is the authorization header to use in request.
 146  // connTimeout is the timeout(in sec) to wait for query output.
 147  func NewHTTPV3Config(url, method, ua, auth string, connTimeout int, it time.Duration, reqIPV4 bool, vhost string, tlsSkipVerify bool, followRedir bool) *Config {
 148  	return &Config{
 149  		"url":             url, // Required
 150  		"method":          method,
 151  		"user_agent":      ua,
 152  		"authorization":   auth,
 153  		"connect_timeout": connTimeout,
 154  		"idle_timeout":    it,
 155  		"require_ipv4":    reqIPV4,
 156  		"virtual_host":    vhost,
 157  		"tls_skip_verify": tlsSkipVerify,
 158  		"follow_redirect": followRedir,
 159  	}
 160  }
 161  
 162  // NewDNSConfig constructs/returns a job configuration for DNS type jobs.
 163  // host is the IP address or hostname of the nameserver to query. (Required)
 164  // domain name to query. (Required)
 165  // port is the dns port to query on host.
 166  // t is the type of the DNS record type to query.
 167  // respTimeout is the timeout(in ms) after sending query to wait for the output.
 168  func NewDNSConfig(host, domain string, port int, t string, respTimeout int) *Config {
 169  	return &Config{
 170  		"host":             host,   // Required
 171  		"domain":           domain, // Required
 172  		"port":             port,
 173  		"type":             t,
 174  		"response_timeout": respTimeout,
 175  	}
 176  }
 177  
 178  // NewTCPConfig constructs/returns a job configuration for TCP type jobs.
 179  // host is the IP address or hostname to connect to. (Required)
 180  // port is the tcp port to connect to on host. (Required)
 181  // connTimeout is the timeout(in ms) before giving up on trying to connect.
 182  // respTimeout is the timeout(in sec) after connecting to wait for output.
 183  // send is the string to send to the host upon connecting.
 184  // ssl determines whether to attempt negotiating an SSL connection.
 185  func NewTCPConfig(host string, port, connTimeout, respTimeout int, send string, ssl bool) *Config {
 186  	return &Config{
 187  		"host":             host, // Required
 188  		"port":             port, // Required
 189  		"connect_timeout":  connTimeout,
 190  		"response_timeout": respTimeout,
 191  		"send":             send,
 192  		"ssl":              ssl,
 193  	}
 194  }
 195  
 196  // NewPINGConfig constructs/returns a job configuration for PING type jobs.
 197  // host is the IP address or hostname to ping. (Required)
 198  // timeout is the timeout(in ms) before marking the host as failed.
 199  // count is the number of packets to send.
 200  // interval is the minimum time(in ms) to wait between sending each packet.
 201  func NewPINGConfig(host string, timeout, count, interval int) *Config {
 202  	return &Config{
 203  		"host":     host, // Required
 204  		"timeout":  timeout,
 205  		"count":    count,
 206  		"interval": interval,
 207  	}
 208  }
 209