...
 
Commits (2)
  • AtjonTV's avatar
    Move Cachet status codes into Cachet package · d238b69a
    AtjonTV authored
    d238b69a
  • AtjonTV's avatar
    Support Sqlite and MySQL · e11f6a92
    AtjonTV authored
    Rather then to just support SQLite keeping MySQL could actually be a benefit as one might have MySQL or MariaDB installed.
    
    If we where to support PostgreSQL everyone with a default Zabbix 5 installation can benefit from the Zabbix PostgreSQL server.
    e11f6a92
......@@ -83,6 +83,7 @@ url = "<cachet url>"
token = "<cachet token>"
[database]
driver="sqlite3"
file="/path/to/store/sqlite.db3"
[components]
......@@ -94,7 +95,17 @@ new_issue = "**Overwatch detected a Critical issue, the Team has been Informed!*
fixed_issue = "**Overwatch detected the Issue was fixed**\n\n*This is a Automated action by The Overwatch**"
```
The messages can be changed, yet they remain generic.
Optionally one can use MySQL/MariaDB:
```
[database]
driver="mysql"
user="root"
pass="root"
name="cachet_bridge"
host="172.17.0.3"
port=3306
```
# License
......
......@@ -3,6 +3,7 @@ url = "<cachet url>"
token = "<cachet token>"
[database]
driver="sqlite3"
file="sqlite.db3"
[components]
......
......@@ -5,7 +5,9 @@ go 1.14
require (
github.com/Masterminds/semver v1.5.0
github.com/andygrunwald/cachet v0.0.0-20180404195246-2a1ca51087fd
github.com/go-sql-driver/mysql v1.5.0
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/smartystreets/goconvey v1.6.4 // indirect
gopkg.in/ini.v1 v1.57.0
)
......@@ -6,5 +6,20 @@ github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gG
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/ini.v1 v1.57.0 h1:9unxIsFcTt4I55uWluz+UmL95q4kdJ0buvQ1ZIqVQww=
gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
......@@ -16,6 +16,15 @@ import (
"gopkg.in/ini.v1"
)
const Unknown = 0
const StatusOperational = 1
const StatusPerformanceIssues = 2
const StatusPartialOutage = 3
const StatusMajorOutage = 4
const StatusInvestigating = 1
const StatusFixed = 4
func Init2(file *ini.File) *cachet.Client {
cachetUrl := file.Section("cachet").Key("url").String()
cachetToken := file.Section("cachet").Key("token").String()
......
......@@ -14,33 +14,48 @@ import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/ini.v1"
)
type Database struct {
db *sql.DB
databaseName string
databaseTable string
isSqlite bool
}
func Init2(file *ini.File) *Database {
dbc := file.Section("database")
host := dbc.Key("host").String()
port := dbc.Key("port").String()
user := dbc.Key("user").String()
pass := dbc.Key("pass").String()
name := dbc.Key("name").String()
driver := dbc.Key("driver").String()
src := fmt.Sprintf("%s:%s@(%s:%s)/%s", user, pass, host, port, name)
isSqlite := false
var sdb *sql.DB
var e error
if driver == "sqlite3" {
dbfile := dbc.Key("file").String()
sdb, e = sql.Open("sqlite3", dbfile)
sdb, e := sql.Open("mysql", src)
isSqlite = true
} else if driver == "mysql" || driver == "mariadb" {
host := dbc.Key("host").String()
port := dbc.Key("port").String()
user := dbc.Key("user").String()
pass := dbc.Key("pass").String()
name := dbc.Key("name").String()
src := fmt.Sprintf("%s:%s@(%s:%s)/%s", user, pass, host, port, name)
sdb, e = sql.Open("mysql", src)
} else {
panic("Unknown Database Driver: "+driver)
}
if e != nil {
panic(e)
}
db := Database{db: sdb, databaseName: name, databaseTable: "issues"}
db := Database{db: sdb, databaseTable: "issues", isSqlite: isSqlite}
return &db
}
......@@ -58,13 +73,18 @@ func (d *Database) Ping() {
}
func (d *Database) Schema() {
//_, e := d.db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", d.databaseTable))
//
//if e != nil {
// panic(e)
//}
intType := "INT"
autoInc := "AUTO_INCREMENT"
if d.isSqlite {
intType = "INTEGER"
autoInc = "AUTOINCREMENT"
}
query := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (id %s PRIMARY KEY %s, event_id %s UNIQUE, component_id %s, incident_id %s, is_open BOOL);",
d.databaseTable, intType, autoInc, intType, intType, intType)
_, e := d.db.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (id INT PRIMARY KEY AUTO_INCREMENT, event_id INT UNIQUE, component_id INT, incident_id INT, is_open BOOL);", d.databaseTable))
_, e := d.db.Exec(query)
if e != nil {
panic(e)
......
......@@ -21,8 +21,6 @@ import (
"strings"
)
const StatusInvestigating = 1
const StatusFixed = 4
func main() {
cfg, e := ini.Load("config.ini")
......@@ -60,9 +58,9 @@ func main() {
var statusId = -1
if strings.Contains(status.Status, "started") {
statusId = StatusInvestigating
statusId = cachet.StatusInvestigating
} else if strings.Contains(status.Status, "resolved") {
statusId = StatusFixed
statusId = cachet.StatusFixed
}
capi := cachet.Init2(cfg)
......@@ -70,7 +68,7 @@ func main() {
newIssueMessage := cfg.Section("messages").Key("new_issue").String()
fixedIssueMessage := cfg.Section("messages").Key("fixed_issue").String()
if statusId == StatusInvestigating && severity == 4 && hostId > 0 {
if statusId == cachet.StatusInvestigating && severity == cachet.StatusMajorOutage && hostId > 0 {
i := cachet2.Incident{
Status: statusId,
ComponentStatus: severity,
......@@ -90,7 +88,7 @@ func main() {
db.OpenIssueForComponent(status.EventId, hostId, i2.ID)
}
if statusId == StatusFixed && hostId > 0 {
if statusId == cachet.StatusFixed && hostId > 0 {
issue := db.GetIssueForComponent(status.EventId)
if issue < 0 {
......@@ -105,8 +103,8 @@ func main() {
}
iu := cachet2.IncidentUpdate{
Status: StatusFixed,
ComponentStatus: 1,
Status: cachet.StatusFixed,
ComponentStatus: cachet.StatusOperational,
ComponentID: hostId,
Message: fixedIssueMessage,
}
......@@ -116,7 +114,7 @@ func main() {
panic(e)
}
c := cachet2.Component{Status: 1}
c := cachet2.Component{Status: cachet.StatusOperational}
_, _, e = capi.Components.Update(hostId, &c)
if e != nil {
panic(e)
......
......@@ -10,6 +10,8 @@ package zabbix
* You can obtain one at https://atvg-studios.com/ospl/ospl20
*/
import "atvg-studios.com/zabbix-cachet-bridge/src/cachet"
type Status struct {
Host string `json:"host"`
Status string `json:"status"`
......@@ -19,21 +21,22 @@ type Status struct {
EventId int `json:"event_id"`
}
func GetCachetSeverity(severity string) int {
switch severity {
case "Disaster":
return 4
return cachet.StatusMajorOutage
case "High":
return 4
return cachet.StatusMajorOutage
case "Average":
return 3
return cachet.StatusPartialOutage
case "Warning":
return 2
return cachet.StatusPerformanceIssues
case "Information":
return 1
return cachet.StatusOperational
case "Not classified":
return 1
return cachet.StatusOperational
}
return -1
return cachet.Unknown
}
\ No newline at end of file