-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsMonitoring.go
More file actions
169 lines (117 loc) · 3.36 KB
/
wsMonitoring.go
File metadata and controls
169 lines (117 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
)
//constant of duration for monitoring, change here for more or less time
const monitorings = 4
const delayMonitoring = 15
//function main.
func main() {
showIntro() //call the function for show the Welcome
showMenu() //call the function for show the Menu
command := readCommand() //var command recive the readCommand
//Use cases
switch command {
case 1:
startMonitoring()
case 2:
fmt.Println("Exibindo Logs")
printLogs()
case 0:
fmt.Println("Saindo do Programa")
os.Exit(0)
default:
fmt.Println("Comando Não reconhecido.")
}
}
//func to Welcome
func showIntro() {
yourName := "Alfredo"
version := 1.1
fmt.Println("Olá, sr.", yourName)
fmt.Println("Este programa ta na versão", version)
}
//func to read user command
func readCommand() int {
var readedCommand int
fmt.Scan(&readedCommand)
fmt.Println("O comando escolhido foi: ", readedCommand)
return readedCommand
}
//func to Menu
func showMenu() {
fmt.Println("1- Iniciar Monitoramento")
fmt.Println("2- Exibir Logs")
fmt.Println("0- Sair do Programa")
}
//this func start and do the monitoring
func startMonitoring() {
fmt.Println("Monitorando...")
sites := readArchiveSites() //call function with site list
for i := 0; i < monitorings; i++ { //Loop For do monitoring of sites
for i, site := range sites {
fmt.Println("Sites monitorados ", i, ": ", site)
testSite(site) // call func test
}
time.Sleep(delayMonitoring * time.Minute) //Monitoring interval
fmt.Println("") //Jump line
}
}
// this func show the status of sites
func testSite(site string) {
resp, err := http.Get(site)
if err != nil { // If a problem show the error
fmt.Println("Ocorreu um erro: ", err)
}
if resp.StatusCode == 200 { // If the site has ok show ok
fmt.Println("Site: ", site, "Foi carregado com sucesso!", resp.StatusCode)
recordLog(site, true) // call to func recordLog
} else { // If the site has a problem show the error status
fmt.Println("Site: ", site, "Não foi possivel carregar. Status Code: ", resp.StatusCode)
recordLog(site, false) // call to func recordLog
}
}
// this func read the archive that has a URL list
func readArchiveSites() []string {
var sites []string // a slice with urls sites
arquivo, err := os.Open("sites.txt")
if err != nil { // If a problem show the error
fmt.Println("Ocorreu um erro: ", err)
}
leitor := bufio.NewReader(arquivo) // command for read archive
for { // loop for read urls in line
linha, err := leitor.ReadString('\n')
linha = strings.TrimSpace(linha)
sites = append(sites, linha)
if err == io.EOF {
break
}
}
arquivo.Close()
return sites
}
// this func record logs
func recordLog(site string, status bool) {
arquivo, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) // this command is for read the archive or create or record the archive with logs
if err != nil {
fmt.Println(err)
}
arquivo.WriteString(time.Now().Format("02/01/2006 15:04:05") + " - " + site + "online: " + strconv.FormatBool(status) + "\n") // this command is for write logs with data, site name and status
arquivo.Close()
}
// this func show logs
func printLogs() {
arquivo, err := ioutil.ReadFile("log.txt")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(arquivo))
}