This repository was archived by the owner on Sep 17, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathfail_log.go
More file actions
144 lines (134 loc) · 3 KB
/
Copy pathfail_log.go
File metadata and controls
144 lines (134 loc) · 3 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
package controllers
import (
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"time"
"github.com/ModuleAB/ModuleAB/server/models"
"github.com/astaxie/beego"
)
func init() {
AddPrivilege("GET", "^/api/v1/records", models.RoleFlagUser)
}
type FailLogContoller struct {
beego.Controller
}
/*
func (h *FailLogContoller) Prepare() {
if h.Ctx.Input.Header("Signature") != "" {
err := common.AuthWithKey(h.Ctx)
if err != nil {
h.Data["json"] = map[string]string{
"error": err.Error(),
}
h.Ctx.Output.SetStatus(http.StatusForbidden)
h.ServeJSON()
}
} else {
id := h.GetSession("id")
if id == nil {
h.Data["json"] = map[string]string{
"error": "You need login first.",
}
h.Ctx.Output.SetStatus(http.StatusUnauthorized)
h.ServeJSON()
} else {
if !CheckPrivileges(id.(string), h.Ctx) {
h.Data["json"] = map[string]string{
"error": "No privileges.",
}
h.Ctx.Output.SetStatus(http.StatusForbidden)
h.ServeJSON()
}
}
}
}
*/
// @Title createFailLog
// @Success 201
// @router / [post]
func (h *FailLogContoller) Post() {
failLog := new(models.FailLog)
defer h.ServeJSON()
err := json.Unmarshal(h.Ctx.Input.RequestBody, failLog)
if err != nil {
beego.Warn("[C] Got error:", err)
h.Data["json"] = map[string]string{
"message": "Bad request",
"error": err.Error(),
}
h.Ctx.Output.SetStatus(http.StatusBadRequest)
return
}
beego.Debug("[C] Got data:", failLog, failLog.Host)
id, err := models.AddFailLog(failLog)
if err != nil {
beego.Warn("[C] Got error:", err)
h.Data["json"] = map[string]string{
"message": "Failed to add New record",
"error": err.Error(),
}
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
return
}
beego.Debug("[C] Got id:", id)
h.Data["json"] = map[string]string{
"id": id,
}
h.Ctx.Output.SetStatus(http.StatusCreated)
fp, err := filepath.Abs(os.Args[0])
if err != nil {
beego.Warn("[C] Got error:", err)
return
}
dir, _ := path.Split(fp)
cmd := exec.Command(
path.Join(dir, "alarm"),
failLog.Host.Name,
failLog.Log,
)
err = cmd.Run()
if err != nil {
beego.Warn("[C] Got error:", err)
return
}
}
// @Title listFailLog
// @Success 200
// @router / [get]
func (h *FailLogContoller) GetAll() {
Time, err := time.ParseInLocation("2006-01-02", h.GetString("time"), time.Local)
beego.Debug("[C] Time:", Time)
host := h.GetString("host")
failLog := new(models.FailLog)
if err == nil {
failLog.Time = Time
}
if host != "" {
failLog.Host = &models.Hosts{
Name: host,
}
}
defer h.ServeJSON()
failLogs, err := models.GetFailLogs(failLog)
if err != nil {
h.Data["json"] = map[string]string{
"message": fmt.Sprint("Failed to get"),
"error": err.Error(),
}
beego.Warn("[C] Got error:", err)
h.Ctx.Output.SetStatus(http.StatusInternalServerError)
return
}
h.Data["json"] = failLogs
if len(failLogs) == 0 {
beego.Debug("[C] Got nothing")
h.Ctx.Output.SetStatus(http.StatusNotFound)
} else {
h.Ctx.Output.SetStatus(http.StatusOK)
}
}