1// Copyright 2016-2020 The Libsacloud Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package sacloud 16 17import "time" 18 19// SimpleMonitor シンプル監視 20type SimpleMonitor struct { 21 *Resource // ID 22 propName // 名称 23 propDescription // 説明 24 propServiceClass // サービスクラス 25 propIcon // アイコン 26 propTags // タグ 27 propCreatedAt // 作成日時 28 propModifiedAt // 変更日時 29 30 Settings *SimpleMonitorSettings `json:",omitempty"` // 設定 31 Status *SimpleMonitorStatus `json:",omitempty"` // ステータス 32 Provider *SimpleMonitorProvider `json:",omitempty"` // プロバイダ 33} 34 35// SimpleMonitorSettings シンプル監視設定 リスト 36type SimpleMonitorSettings struct { 37 SimpleMonitor *SimpleMonitorSetting `json:",omitempty"` // シンプル監視設定値 38} 39 40// SimpleMonitorSetting シンプル監視設定 41type SimpleMonitorSetting struct { 42 DelayLoop int `json:",omitempty"` // 監視間隔 43 NotifyInterval int `json:",omitempty"` // 再通知間隔(秒数) 44 HealthCheck *SimpleMonitorHealthCheck `json:",omitempty"` // ヘルスチェック 45 Enabled string `json:",omitempty"` // 有効/無効 46 NotifyEmail *SimpleMonitorNotify `json:",omitempty"` // Email通知 47 NotifySlack *SimpleMonitorNotify `json:",omitempty"` // Slack通知 48} 49 50// SimpleMonitorStatus シンプル監視ステータス 51type SimpleMonitorStatus struct { 52 Target string `json:",omitempty"` // 対象(IP or FQDN) 53} 54 55// SimpleMonitorProvider プロバイダ 56type SimpleMonitorProvider struct { 57 *Resource // ID 58 propName // 名称 59 propServiceClass // サービスクラス 60 61 Class string `json:",omitempty"` // クラス 62} 63 64// SimpleMonitorHealthCheck ヘルスチェック 65type SimpleMonitorHealthCheck struct { 66 Protocol string `json:",omitempty"` // プロトコル 67 Port string `json:",omitempty"` // ポート 68 Path string `json:",omitempty"` // HTTP/HTTPS監視の場合のリクエストパス 69 Status string `json:",omitempty"` // HTTP/HTTPS監視の場合の期待ステータスコード 70 SNI string `json:",omitempty"` // HTTPS監視時のSNI有効/無効 71 Host string `json:",omitempty"` // 対象ホスト(IP or FQDN) 72 BasicAuthUsername string `json:",omitempty"` // HTTP/HTTPS監視の場合のBASIC認証 ユーザー名 73 BasicAuthPassword string `json:",omitempty"` // HTTP/HTTPS監視の場合のBASIC認証 パスワード 74 QName string `json:",omitempty"` // DNS監視の場合の問い合わせFQDN 75 ExpectedData string `json:",omitempty"` // 期待値 76 Community string `json:",omitempty"` // SNMP監視の場合のコミュニティ名 77 SNMPVersion string `json:",omitempty"` // SNMP監視 SNMPバージョン 78 OID string `json:",omitempty"` // SNMP監視 OID 79 RemainingDays int `json:",omitempty"` // SSL証明書 有効残日数 80} 81 82// SimpleMonitorNotify シンプル監視通知 83type SimpleMonitorNotify struct { 84 Enabled string `json:",omitempty"` // 有効/無効 85 HTML string `json:",omitempty"` // メール通知の場合のHTMLメール有効フラグ 86 IncomingWebhooksURL string `json:",omitempty"` // Slack通知の場合のWebhook URL 87} 88 89// ESimpleMonitorHealth シンプル監視ステータス 90type ESimpleMonitorHealth string 91 92var ( 93 // EHealthUp Up 94 EHealthUp = ESimpleMonitorHealth("UP") 95 // EHealthDown Down 96 EHealthDown = ESimpleMonitorHealth("DOWN") 97) 98 99// IsUp アップ 100func (e ESimpleMonitorHealth) IsUp() bool { 101 return e == EHealthUp 102} 103 104// IsDown ダウン 105func (e ESimpleMonitorHealth) IsDown() bool { 106 return e == EHealthDown 107} 108 109// SimpleMonitorHealthCheckStatus シンプル監視ステータス 110type SimpleMonitorHealthCheckStatus struct { 111 LastCheckedAt time.Time 112 LastHealthChangedAt time.Time 113 Health ESimpleMonitorHealth 114} 115 116// CreateNewSimpleMonitor シンプル監視作成 117func CreateNewSimpleMonitor(target string) *SimpleMonitor { 118 return &SimpleMonitor{ 119 propName: propName{Name: target}, 120 Provider: &SimpleMonitorProvider{ 121 Class: "simplemon", 122 }, 123 Status: &SimpleMonitorStatus{ 124 Target: target, 125 }, 126 Settings: &SimpleMonitorSettings{ 127 SimpleMonitor: &SimpleMonitorSetting{ 128 HealthCheck: &SimpleMonitorHealthCheck{}, 129 Enabled: "True", 130 NotifyEmail: &SimpleMonitorNotify{ 131 Enabled: "False", 132 }, 133 NotifySlack: &SimpleMonitorNotify{ 134 Enabled: "False", 135 }, 136 }, 137 }, 138 } 139 140} 141 142// AllowSimpleMonitorHealthCheckProtocol シンプル監視対応プロトコルリスト 143func AllowSimpleMonitorHealthCheckProtocol() []string { 144 return []string{"http", "https", "ping", "tcp", "dns", "ssh", "smtp", "pop3", "snmp", "sslcertificate"} 145} 146 147func createSimpleMonitorNotifyEmail(withHTML bool) *SimpleMonitorNotify { 148 n := &SimpleMonitorNotify{ 149 Enabled: "True", 150 HTML: "False", 151 } 152 153 if withHTML { 154 n.HTML = "True" 155 } 156 157 return n 158} 159 160func createSimpleMonitorNotifySlack(incomingWebhooksURL string) *SimpleMonitorNotify { 161 return &SimpleMonitorNotify{ 162 Enabled: "True", 163 IncomingWebhooksURL: incomingWebhooksURL, 164 } 165 166} 167 168// SetTarget 対象ホスト(IP or FQDN)の設定 169func (s *SimpleMonitor) SetTarget(target string) { 170 s.Name = target 171 s.Status.Target = target 172} 173 174// SetHealthCheckPing pingでのヘルスチェック設定 175func (s *SimpleMonitor) SetHealthCheckPing() { 176 s.Settings.SimpleMonitor.HealthCheck = &SimpleMonitorHealthCheck{ 177 Protocol: "ping", 178 } 179} 180 181// SetHealthCheckTCP TCPでのヘルスチェック設定 182func (s *SimpleMonitor) SetHealthCheckTCP(port string) { 183 s.Settings.SimpleMonitor.HealthCheck = &SimpleMonitorHealthCheck{ 184 Protocol: "tcp", 185 Port: port, 186 } 187} 188 189// SetHealthCheckHTTP HTTPでのヘルスチェック設定 190func (s *SimpleMonitor) SetHealthCheckHTTP(port string, path string, status string, host string, user, pass string) { 191 s.Settings.SimpleMonitor.HealthCheck = &SimpleMonitorHealthCheck{ 192 Protocol: "http", 193 Port: port, 194 Path: path, 195 Status: status, 196 Host: host, 197 BasicAuthUsername: user, 198 BasicAuthPassword: pass, 199 } 200} 201 202// SetHealthCheckHTTPS HTTPSでのヘルスチェック設定 203func (s *SimpleMonitor) SetHealthCheckHTTPS(port string, path string, status string, host string, sni bool, user, pass string) { 204 strSNI := "False" 205 if sni { 206 strSNI = "True" 207 } 208 s.Settings.SimpleMonitor.HealthCheck = &SimpleMonitorHealthCheck{ 209 Protocol: "https", 210 Port: port, 211 Path: path, 212 Status: status, 213 Host: host, 214 SNI: strSNI, 215 BasicAuthUsername: user, 216 BasicAuthPassword: pass, 217 } 218} 219 220// SetHealthCheckDNS DNSクエリでのヘルスチェック設定 221func (s *SimpleMonitor) SetHealthCheckDNS(qname string, expectedData string) { 222 s.Settings.SimpleMonitor.HealthCheck = &SimpleMonitorHealthCheck{ 223 Protocol: "dns", 224 QName: qname, 225 ExpectedData: expectedData, 226 } 227} 228 229// SetHealthCheckSSH SSHヘルスチェック設定 230func (s *SimpleMonitor) SetHealthCheckSSH(port string) { 231 s.Settings.SimpleMonitor.HealthCheck = &SimpleMonitorHealthCheck{ 232 Protocol: "ssh", 233 Port: port, 234 } 235} 236 237// SetHealthCheckSMTP SMTPヘルスチェック設定 238func (s *SimpleMonitor) SetHealthCheckSMTP(port string) { 239 s.Settings.SimpleMonitor.HealthCheck = &SimpleMonitorHealthCheck{ 240 Protocol: "smtp", 241 Port: port, 242 } 243} 244 245// SetHealthCheckPOP3 POP3ヘルスチェック設定 246func (s *SimpleMonitor) SetHealthCheckPOP3(port string) { 247 s.Settings.SimpleMonitor.HealthCheck = &SimpleMonitorHealthCheck{ 248 Protocol: "pop3", 249 Port: port, 250 } 251} 252 253// SetHealthCheckSNMP SNMPヘルスチェック設定 254func (s *SimpleMonitor) SetHealthCheckSNMP(community string, version string, oid string, expectedData string) { 255 s.Settings.SimpleMonitor.HealthCheck = &SimpleMonitorHealthCheck{ 256 Protocol: "snmp", 257 Community: community, 258 SNMPVersion: version, 259 OID: oid, 260 ExpectedData: expectedData, 261 } 262} 263 264// SetHealthCheckSSLCertificate SSLサーバ証明書有効期限ヘルスチェック設定 265func (s *SimpleMonitor) SetHealthCheckSSLCertificate(remainingDays int) { 266 // set default 267 if remainingDays < 0 { 268 remainingDays = 30 269 } 270 s.Settings.SimpleMonitor.HealthCheck = &SimpleMonitorHealthCheck{ 271 Protocol: "sslcertificate", 272 RemainingDays: remainingDays, 273 } 274} 275 276// EnableNotifyEmail Email通知の有効か 277func (s *SimpleMonitor) EnableNotifyEmail(withHTML bool) { 278 s.Settings.SimpleMonitor.NotifyEmail = createSimpleMonitorNotifyEmail(withHTML) 279} 280 281// DisableNotifyEmail Email通知の無効化 282func (s *SimpleMonitor) DisableNotifyEmail() { 283 s.Settings.SimpleMonitor.NotifyEmail = &SimpleMonitorNotify{ 284 Enabled: "False", 285 } 286} 287 288// EnableNofitySlack Slack通知の有効化 289func (s *SimpleMonitor) EnableNofitySlack(incomingWebhooksURL string) { 290 s.Settings.SimpleMonitor.NotifySlack = createSimpleMonitorNotifySlack(incomingWebhooksURL) 291} 292 293// DisableNotifySlack Slack通知の無効化 294func (s *SimpleMonitor) DisableNotifySlack() { 295 s.Settings.SimpleMonitor.NotifySlack = &SimpleMonitorNotify{ 296 Enabled: "False", 297 } 298} 299 300// SetDelayLoop 監視間隔の設定 301func (s *SimpleMonitor) SetDelayLoop(loop int) { 302 s.Settings.SimpleMonitor.DelayLoop = loop 303} 304 305// SetNotifyInterval 再通知間隔の設定 306func (s *SimpleMonitor) SetNotifyInterval(loop int) { 307 s.Settings.SimpleMonitor.NotifyInterval = loop 308} 309