mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
Refactored flags and added xnames to add endpoint to hms-smd
This commit is contained in:
parent
de07b119b5
commit
2edb9fdbb0
18 changed files with 270 additions and 175 deletions
1
internal/db/postgresql/postgresql.go
Normal file
1
internal/db/postgresql/postgresql.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
package postgresql
|
||||
60
internal/db/sqlite/sqlite.go
Normal file
60
internal/db/sqlite/sqlite.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package sqlite
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
magellan "davidallendj/magellan/internal"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
func InsertProbeResults(path string, states *[]magellan.BMCProbeResult) error {
|
||||
if states == nil {
|
||||
return fmt.Errorf("states == nil")
|
||||
}
|
||||
|
||||
// create database if it doesn't already exist
|
||||
schema := `
|
||||
CREATE TABLE IF NOT EXISTS magellan_scanned_ports (
|
||||
host TEXT PRIMARY KEY NOT NULL,
|
||||
port INTEGER,
|
||||
protocol TEXT,
|
||||
state INTEGER
|
||||
);
|
||||
`
|
||||
db, err := sqlx.Open("sqlite3", path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not open database: %v", err)
|
||||
}
|
||||
db.MustExec(schema)
|
||||
|
||||
// insert all probe states into db
|
||||
tx := db.MustBegin()
|
||||
for _, state := range *states {
|
||||
sql := `INSERT OR REPLACE INTO magellan_scanned_ports (host, port, protocol, state)
|
||||
VALUES (:host, :port, :protocol, :state);`
|
||||
_, err := tx.NamedExec(sql, &state)
|
||||
if err != nil {
|
||||
fmt.Printf("could not execute transaction: %v\n", err)
|
||||
}
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not commit transaction: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetProbeResults(path string) ([]magellan.BMCProbeResult, error) {
|
||||
db, err := sqlx.Open("sqlite3", path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not open database: %v", err)
|
||||
}
|
||||
|
||||
results := []magellan.BMCProbeResult{}
|
||||
err = db.Select(&results, "SELECT * FROM magellan_scanned_ports ORDER BY host ASC")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not retrieve probes: %v", err)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue