summaryrefslogtreecommitdiff
path: root/xe-daemon/xe-daemon.go
blob: 3ad8c27e069e136a71a4db3d1249d3391c4836e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main

import (
	guestmetric "../guestmetric"
	xenstoreclient "../xenstoreclient"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/signal"
	"strconv"
	"syscall"
	"time"
)

const (
	LoggerName string = "xe-daemon"
)

func main() {
	var err error

	sleepInterval := flag.Int("i", 60, "Interval between updates (in seconds)")
	debugFlag := flag.Bool("d", false, "Update to stdout rather than xenstore")
	balloonFlag := flag.Bool("B", true, "Do not report that ballooning is supported")
	pid := flag.String("p", "", "Write the PID to FILE")

	flag.Parse()

	if *pid != "" {
		if err = ioutil.WriteFile(*pid, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
			fmt.Fprintf(os.Stderr, "Write pid to %s error: %s\n", *pid, err)
			return
		}
	}

	var loggerWriter io.Writer = os.Stderr
	var topic string = LoggerName
	if w, err := NewSyslogWriter(topic); err == nil {
		loggerWriter = w
		topic = ""
	} else {
		fmt.Fprintf(os.Stderr, "NewSyslogWriter(%s) error: %s, use stderr logging\n", topic, err)
		topic = LoggerName + ": "
	}

	logger := log.New(loggerWriter, topic, 0)

	exitChannel := make(chan os.Signal, 1)
	signal.Notify(exitChannel, syscall.SIGTERM, syscall.SIGINT)

	xs, err := xenstoreclient.NewCachedXenstore(0)
	if err != nil {
		message := fmt.Sprintf("NewCachedXenstore error: %v\n", err)
		logger.Print(message)
		fmt.Fprint(os.Stderr, message)
		return
	}

	collector := &guestmetric.Collector{
		Client: xs,
		Ballon: *balloonFlag,
		Debug:  *debugFlag,
	}

	collectors := []struct {
		divisor int
		name    string
		Collect func() (guestmetric.GuestMetric, error)
	}{
		{1, "CollectOS", collector.CollectOS},
		{1, "CollectMisc", collector.CollectMisc},
		{1, "CollectNetworkAddr", collector.CollectNetworkAddr},
		{1, "CollectDisk", collector.CollectDisk},
		{2, "CollectMemory", collector.CollectMemory},
	}

	lastUniqueID, err := xs.Read("unique-domain-id")
	if err != nil {
		logger.Printf("xenstore.Read unique-domain-id error: %v\n", err)
	}

	for count := 0; ; count += 1 {
		uniqueID, err := xs.Read("unique-domain-id")
		if err != nil {
			logger.Printf("xenstore.Read unique-domain-id error: %v\n", err)
			return
		}
		if uniqueID != lastUniqueID {
			// VM has just resume, cache state now invalid
			lastUniqueID = uniqueID
			if cx, ok := xs.(*xenstoreclient.CachedXenStore); ok {
				cx.Clear()
			}
		}

		// invoke collectors
		updated := false
		for _, collector := range collectors {
			if count%collector.divisor == 0 {
				logger.Printf("Running %s ...\n", collector.name)
				result, err := collector.Collect()
				if err != nil {
					logger.Printf("%s error: %#v\n", collector.name, err)
				} else {
					for name, value := range result {
						err := xs.Write(name, value)
						if err != nil {
							logger.Printf("xenstore.Write error: %v\n", err)
						} else {
							if *debugFlag {
								logger.Printf("xenstore.Write OK: %#v: %#v\n", name, value)
							}
							updated = true
						}
					}
				}
			}
		}

		if updated {
			xs.Write("data/updated", time.Now().Format("Mon Jan _2 15:04:05 2006"))
		}

		select {
		case <-exitChannel:
			logger.Printf("Received an interrupt, stopping services...\n")
			if c, ok := loggerWriter.(io.Closer); ok {
				if err := c.Close(); err != nil {
					fmt.Fprintf(os.Stderr, "logger close error: %s\n", err)
				}
			}
			return

		case <-time.After(time.Duration(*sleepInterval) * time.Second):
			continue
		}
	}
}