Beispiel #1
0
func PingHost(host string) {
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, os.Interrupt)
	tick := time.NewTicker(1 * time.Second)

	for {
		select {
		case <-tick.C:
			startTime := time.Now()
			pr, err := ping.Ping(host)

			if err != nil {
				if te, ok := err.(ping.TimeoutError); ok {
					fmt.Println(err)
					err = d.SavePing(te.IP(), startTime, -1)
					if err != nil {
						log.Fatal(err)
					}
				} else {
					log.Fatal(err)
				}
			} else {
				fmt.Printf("%.3f \n", pr.Time)
				err = d.SavePing(pr.IP, startTime, float32(pr.Time))
			}
			if err != nil {
				log.Fatal(err)
			}
		case <-signalChan:
			os.Exit(0)
		}
	}
}
Beispiel #2
0
func Test_main_integration(t *testing.T) {
	Convey("Should ping localhost once and save to db", t, func() {
		Reset(func() {
			os.Remove("pinghist.db")
		})
		ip := "127.0.0.1"
		startTime := time.Now()

		pr, err := ping.Ping(ip)
		So(pr, ShouldNotBeNil)
		So(err, ShouldBeNil)

		d := dal.NewDAL()
		d.CreateBuckets()
		err = d.SavePing(ip, startTime, float32(pr.Time))
		So(err, ShouldBeNil)

		groups, err := d.GetPings(ip, startTime, startTime.Add(1*time.Minute), 1*time.Hour)

		So(err, ShouldBeNil)
		So(len(groups), ShouldEqual, 1)
		So(groups[0].MaxTime, ShouldEqual, pr.Time)
		So(groups[0].MinTime, ShouldEqual, pr.Time)
		So(groups[0].AvgTime, ShouldEqual, pr.Time)
		So(groups[0].StdDev, ShouldEqual, 0)
		So(groups[0].Received, ShouldEqual, 1)
		So(groups[0].Timedout, ShouldEqual, 0)
		So(groups[0].TotalTime, ShouldEqual, pr.Time)
	})
}