forked from ASWATFZLLC/telegraf-freeswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (130 loc) · 3.72 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"github.com/gin-gonic/gin"
"github.com/rif/telegraf-freeswitch/utils"
"log"
"net/http"
"os"
"strconv"
)
var (
host = flag.String("host", "localhost", "freeswitch host address")
port = flag.Int("port", 8021, "freeswitch port")
pass = flag.String("pass", "ClueCon", "freeswitch password")
serve = flag.Bool("serve", false, "run as a server")
listenAddress = flag.String("listen_address", "127.0.0.1", "listen on address")
listenPort = flag.Int("listen_port", 9191, "listen on port")
)
func handler(w http.ResponseWriter, route string) {
}
func main() {
flag.Parse()
fetcher, err := utils.NewFetcher(*host, *port, *pass)
if err != nil {
fmt.Print("error connecting to fs: ", err)
}
defer fetcher.Close()
if !*serve {
if err := fetcher.GetData(); err != nil {
fmt.Print(err.Error())
}
fmt.Print(fetcher.FormatOutput(utils.InfluxFormat))
os.Exit(0)
}
r := gin.Default()
r.GET("/status", func(c *gin.Context) {
fetcher, err = utils.NewFetcher(*host, *port, *pass)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer fetcher.Close()
if err := fetcher.GetData(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
status, _, _ := fetcher.FormatOutput(utils.JSONFormat)
c.String(http.StatusOK, status)
})
r.GET("/profiles", func(c *gin.Context) {
fetcher, err = utils.NewFetcher(*host, *port, *pass)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer fetcher.Close()
if err := fetcher.GetData(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, fetcher.SofiaProfiles)
})
r.GET("/gateways", func(c *gin.Context) {
fetcher, err = utils.NewFetcher(*host, *port, *pass)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer fetcher.Close()
if err := fetcher.GetData(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, fetcher.SofiaGateways)
})
r.GET("/gateways/check", func(c *gin.Context) {
fetcher, err = utils.NewFetcher(*host, *port, *pass)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer fetcher.Close()
if err := fetcher.GetData(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
globalStatus := true
for _, sofiaGateway := range fetcher.SofiaGateways {
if sofiaGateway.Status == "0" {
globalStatus = false
}
}
if globalStatus == true {
c.JSON(http.StatusOK, fetcher.SofiaGateways)
} else {
c.JSON(http.StatusInternalServerError, fetcher.SofiaGateways)
}
})
r.GET("/gateways/check-latency", func(c *gin.Context) {
fetcher, err = utils.NewFetcher(*host, *port, *pass)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer fetcher.Close()
if err := fetcher.GetData(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
globalStatus := true
for _, sofiaGateway := range fetcher.SofiaGateways {
if sofiaGateway.Status == "0" {
globalStatus = false
}
ping, _ := strconv.ParseFloat(sofiaGateway.Ping, 8)
if ping > 220 {
globalStatus = false
}
}
if globalStatus == true {
c.JSON(http.StatusOK, fetcher.SofiaGateways)
} else {
c.JSON(http.StatusInternalServerError, fetcher.SofiaGateways)
}
})
listen := fmt.Sprintf("%s:%d", *listenAddress, *listenPort)
fmt.Printf("Listening on %s...", listen)
log.Fatal(r.Run(listen))
}