-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.go
More file actions
143 lines (115 loc) · 2.38 KB
/
Copy pathhello.go
File metadata and controls
143 lines (115 loc) · 2.38 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
package main
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
)
const monitoring = 3
const delay = 5
func main() {
registraLog("site-falso", false)
readFile()
exibeIntroducao()
for {
exibeMenu()
command := readCommand()
switch command {
case 1:
startMonitoring()
case 2:
showLogs()
printLog()
case 0:
fmt.Println("Saindo do programa...")
os.Exit(0)
default:
fmt.Println("Nao conheco este comando")
os.Exit(-1)
}
}
}
func exibeIntroducao() {
name := "Matheus"
version := 1.1
fmt.Println("Ola,", name)
fmt.Println("Este programa esta na versao:", version)
}
func readCommand() int {
var readCommand int
fmt.Scan(&readCommand)
fmt.Println("O comando escolhido foi", readCommand)
return readCommand
}
func exibeMenu() {
fmt.Println("1 - Iniciar monitoramento")
fmt.Println("2 - Exibir logs")
fmt.Println("0 - Sair do programa")
}
func startMonitoring() {
fmt.Println("Monitorando...")
sites := readFile()
for i := 0; i < monitoring; i++ {
for i, site := range sites {
fmt.Println("Testando site", i, ":", site)
testSite(site)
}
time.Sleep(delay * time.Second)
fmt.Println("")
}
fmt.Println("")
}
func showLogs() {
fmt.Println("Exibindo Logs...")
}
func testSite(site string) {
resp, err := http.Get(site)
if err != nil {
fmt.Println("Ocorreu um erro")
}
if resp.StatusCode == 200 {
fmt.Println("O site:", site, "foi carregado com sucesso!")
registraLog(site, true)
} else {
fmt.Println("O site:", site, "esta com problemas. Status Code:", resp.StatusCode)
registraLog(site, false)
}
}
func readFile() []string {
var sites []string
file, err := os.Open("sites.txt")
if err != nil {
fmt.Println("Ocorreu um erro", err)
}
reader := bufio.NewReader(file)
for {
linha, err := reader.ReadString('\n')
linha = strings.TrimSpace(linha)
sites = append(sites, linha)
if err == io.EOF {
break
}
}
file.Close()
return sites
}
func registraLog(site string, status bool) {
file, err := os.OpenFile("log.txt", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
fmt.Println("Ocorreu um erro")
}
file.WriteString(time.Now().Format("02/01/2006 15:04:05")+ " - " + site + " - " + strconv.FormatBool(status) + "\n")
fmt.Println(file)
file.Close()
}
func printLog(){
file, err := os.ReadFile("log.txt")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(file))
}