Developing lightweight computation at the DSG edge

Commit 1ab69a53 authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[dump-monitor-assign] A dummy tool to dump assignations


Signed-off-by: Roger Pueyo Centelles's avatarRoger Pueyo Centelles <rpueyo@ac.upc.edu>
parent 35b4083f
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/signal"
"time"
"uc-monitor-go-test/cnml"
"uc-monitor-go-test/goclidote"
"github.com/golang/glog"
)
type JSONDeviceMonitorAssignation struct {
Device string `json:"device"`
Monitors []string `json:"monitors"`
}
type JSONAllData struct {
Devices []string `json:"devices"`
Monitors []string `json:"monitors"`
Assignations []JSONDeviceMonitorAssignation `json:"assignations"`
}
// Define error levels
// FATAL output code
var FATAL = 1
// ERROR output code
var ERROR = 2
// WARNING output code
var WARNING = 3
// INFO output code
var INFO = 4
// DEFAULTFILEMODE for created files
var DEFAULTFILEMODE os.FileMode = 0755
//Default settings and descriptions
var dbHost = "localhost"
var dbHostHelp = "The hostname or IP address where AntidoteDB is running"
var dbPort = 8087
var dbPortHelp = "The TCP port on which AntidoteDB is listening"
var globalAssignCheckInterval = 5 //60
var globalAssignCheckIntervalHelp = "Interval (in seconds) in which to check for changes in the local devices assignation list"
// Global data structures
var globalAssign []cnml.DeviceMonitorAssignation
var monitorsList []string
var devicesList []string
func main() {
//Initialize the service (read the configuration flags, etc.)
initialize()
// Handle interrupt signals in order to terminate gracefully
signalChan := make(chan os.Signal, 1)
cleanupDone := make(chan bool)
signal.Notify(signalChan, os.Interrupt)
go func() {
for _ = range signalChan {
fmt.Println("\nReceived an interrupt, quitting...")
cleanupDone <- true
}
}()
<-cleanupDone
fmt.Println("\nBye!")
}
// The initialize() function manages the execution parameters, sets timers, etc.
func initialize() {
fmt.Println("Initializing...")
// Manage command line flags and execution settings
initializeFlags()
// Initialize periodic timers
initializeTimers()
// The application is now ininitialized; entering the loop
fmt.Println("Initialization done. Entering infinite loop...")
}
func initializeTimers() {
// Initialize the local assignation check timer
startGlobalAssignTimer()
}
// The initializeFlags function initializes the command line flags
func initializeFlags() {
//Define and parse command line flags
dbHostPtr := flag.String("db_host", dbHost, dbHostHelp)
dbPortPtr := flag.Int("db_port", dbPort, dbPortHelp)
globalAssignCheckIntervalPtr := flag.Int("globalAssignCheckInterval", globalAssignCheckInterval, globalAssignCheckIntervalHelp)
flag.Parse()
dbHost = *dbHostPtr
dbPort = *dbPortPtr
if *globalAssignCheckIntervalPtr > 0 {
globalAssignCheckInterval = *globalAssignCheckIntervalPtr
}
}
func getCnmlDevicesList() []cnml.DeviceIpv4sGraphserver {
return goclidote.ReadDevicesFromAntidote(dbHost, dbPort)
}
func getGlobalAssignationList() []cnml.DeviceMonitorAssignation {
var newAssign []cnml.DeviceMonitorAssignation
for _, v := range getCnmlDevicesList() {
{
var newDev cnml.DeviceMonitorAssignation
newDev.Device.ID = v.ID
newDev.Device.Addresses = v.Addresses
newDev.MonitorID = goclidote.AntidoteReadItemsFromSetInBucket(fmt.Sprintf("device-%d", v.ID), "monitors", dbHost, dbPort)
newAssign = append(newAssign, newDev)
}
}
return newAssign
}
func startGlobalAssignTimer() {
globalAssignTicker := time.NewTicker(time.Duration(globalAssignCheckInterval) * time.Second)
go func() {
for range globalAssignTicker.C {
fmt.Println("Getting monitors list...")
monitorsList = getCurrentMonitorsList()
fmt.Println("The current monitors list contains", len(monitorsList), "monitors")
fmt.Println("Getting devices list...")
devicesList = getCurrentDevicesList()
fmt.Println("The current devices list contains", len(devicesList), "devices")
fmt.Println("Getting global assignation list...")
globalAssign = getGlobalAssignationList()
fmt.Println("The global assignation list contains", len(globalAssign), "devices")
var dataToExport JSONAllData
dataToExport.Devices = devicesList
dataToExport.Monitors = monitorsList
for _, v := range globalAssign {
var newAssg JSONDeviceMonitorAssignation
newAssg.Device = fmt.Sprintf("%d", v.Device.ID)
newAssg.Monitors = v.MonitorID
dataToExport.Assignations = append(dataToExport.Assignations, newAssg)
}
outFile, _ := os.Create("/tmp/out.json")
defer outFile.Close()
jnodes, _ := json.Marshal(dataToExport)
//fmt.Println(string(jnode))
//outFile.WriteString(string(jnodes))
outFile.WriteString(fmt.Sprintf("data = %s", string(jnodes)))
}
}()
}
func getCurrentMonitorsList() []string {
return goclidote.AntidoteReadItemsFromSetInBucket("guifi", "monitors", dbHost, dbPort)
}
func getCurrentDevicesList() []string {
allDevs := goclidote.ReadDevicesFromAntidote(dbHost, dbPort)
var devsID []string
for _, v := range allDevs {
devsID = append(devsID, fmt.Sprintf("%d", v.ID))
}
return devsID
}
func errCheck(e error, level int, message string) {
if message == "" {
message = "Unknown error"
}
if e != nil {
switch level {
case FATAL:
glog.Fatalln(message, e)
case ERROR:
glog.Errorln(message, e)
case WARNING:
glog.Warningln(message, e)
default:
glog.Infoln(message, e)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment