Пример #1
0
func doDump(inventoryPath, discoveryPath string) (err error) {
	serviceCache, err := scamp.NewServiceCache(discoveryPath)
	if err != nil {
		fmt.Println("could not create service cache:", err.Error())
		return
	}
	serviceCache.Scan()

	eif, err := w.ExpectedInventoryFileFromServiceCache(serviceCache)
	if err != nil {
		panic(err.Error())
	}

	inventoryFile, err := os.Create(inventoryPath)
	if err != nil {
		panic(err.Error())
	}

	indentedJson, err := json.MarshalIndent(eif, "", "  ")
	// json.NewEncoder(inventoryFile).Encode(eif)
	bytesWritten, err := inventoryFile.Write(indentedJson)
	if err != nil {
		panic(err.Error())
	} else if bytesWritten != len(indentedJson) {
		panic("did not write all data")
	}
	inventoryFile.Close()

	return
}
Пример #2
0
func NewInventory(cachePath string) (i *Inventory, err error) {
	i = new(Inventory)
	i.Cache, err = scamp.NewServiceCache(cachePath)
	if err != nil {
		return
	}
	i.Inventory = make(InventoryType)
	return
}
Пример #3
0
func doWatch(inventoryPath, discoveryPath string, scanWait int, pagerDutyKeyPath string) (err error) {
	tracker := w.NewWatchdogTracker()
	expectedInventory, err := w.LoadExpectedInventoryFromFile(inventoryPath)
	if err != nil {
		fmt.Printf("could not load expected inventory: `%s`\n", err)
		return
	}

	serviceCache, err := scamp.NewServiceCache(discoveryPath)
	if err != nil {
		fmt.Println("could not create service cache:", err.Error())
		return
	}

	/*
	   The first pass on the file
	*/
	err = doScanAndTrack(&tracker, serviceCache)
	if err != nil {
		fmt.Println("scan and compare failed:", err.Error())
		return
	}

ScanLoop:
	for {
		select {
		case <-time.After(time.Duration(scanWait) * time.Second):
			err = doScanAndTrack(&tracker, serviceCache)
			if err != nil {
				fmt.Println("scan and compare failed:", err.Error())
				break ScanLoop
			}

			doDiff(&tracker, &expectedInventory, pagerDutyKeyPath)

			tracker.MarkEpoch()
		}
	}

	fmt.Printf("DONE!\n")

	return
}