mirror of
				https://github.com/ClusterCockpit/cc-metric-collector.git
				synced 2025-11-04 02:35:07 +01:00 
			
		
		
		
	CLI option for PID file and fix for DefaultTags
This commit is contained in:
		@@ -66,10 +66,12 @@ func ReadCli() map[string]string {
 | 
				
			|||||||
	var m map[string]string
 | 
						var m map[string]string
 | 
				
			||||||
	cfg := flag.String("config", "./config.json", "Path to configuration file")
 | 
						cfg := flag.String("config", "./config.json", "Path to configuration file")
 | 
				
			||||||
	logfile := flag.String("log", "stderr", "Path for logfile")
 | 
						logfile := flag.String("log", "stderr", "Path for logfile")
 | 
				
			||||||
 | 
						pidfile := flag.String("pidfile", "/var/run/cc-metric-collector.pid", "Path for PID file")
 | 
				
			||||||
	flag.Parse()
 | 
						flag.Parse()
 | 
				
			||||||
	m = make(map[string]string)
 | 
						m = make(map[string]string)
 | 
				
			||||||
	m["configfile"] = *cfg
 | 
						m["configfile"] = *cfg
 | 
				
			||||||
	m["logfile"] = *logfile
 | 
						m["logfile"] = *logfile
 | 
				
			||||||
 | 
						m["pidfile"] = *pidfile
 | 
				
			||||||
	return m
 | 
						return m
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -89,9 +91,28 @@ func SetLogging(logfile string) error {
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func CreatePidfile(pidfile string) error {
 | 
				
			||||||
 | 
					    file, err := os.OpenFile(pidfile, os.O_CREATE|os.O_RDWR, 0600)
 | 
				
			||||||
 | 
					    if err != nil {
 | 
				
			||||||
 | 
					        log.Print(err)
 | 
				
			||||||
 | 
					        return err
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    file.Write([]byte(fmt.Sprintf("%d", os.Getpid())))
 | 
				
			||||||
 | 
					    file.Close()
 | 
				
			||||||
 | 
					    return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func RemovePidfile(pidfile string) error {
 | 
				
			||||||
 | 
					    info, err := os.Stat(pidfile)
 | 
				
			||||||
 | 
					    if !os.IsNotExist(err) && !info.IsDir() {
 | 
				
			||||||
 | 
					        os.Remove(pidfile)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Register an interrupt handler for Ctrl+C and similar. At signal,
 | 
					// Register an interrupt handler for Ctrl+C and similar. At signal,
 | 
				
			||||||
// all collectors are closed
 | 
					// all collectors are closed
 | 
				
			||||||
func shutdown(wg *sync.WaitGroup, config *GlobalConfig, sink sinks.SinkFuncs, recv receivers.ReceiverFuncs) {
 | 
					func shutdown(wg *sync.WaitGroup, config *GlobalConfig, sink sinks.SinkFuncs, recv receivers.ReceiverFuncs, pidfile string) {
 | 
				
			||||||
	sigs := make(chan os.Signal, 1)
 | 
						sigs := make(chan os.Signal, 1)
 | 
				
			||||||
	signal.Notify(sigs, os.Interrupt)
 | 
						signal.Notify(sigs, os.Interrupt)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -108,6 +129,7 @@ func shutdown(wg *sync.WaitGroup, config *GlobalConfig, sink sinks.SinkFuncs, re
 | 
				
			|||||||
			recv.Close()
 | 
								recv.Close()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		sink.Close()
 | 
							sink.Close()
 | 
				
			||||||
 | 
							RemovePidfile(pidfile)
 | 
				
			||||||
		wg.Done()
 | 
							wg.Done()
 | 
				
			||||||
	}(wg)
 | 
						}(wg)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -125,6 +147,7 @@ func main() {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	clicfg := ReadCli()
 | 
						clicfg := ReadCli()
 | 
				
			||||||
 | 
						err = CreatePidfile(clicfg["pidfile"])
 | 
				
			||||||
	err = SetLogging(clicfg["logfile"])
 | 
						err = SetLogging(clicfg["logfile"])
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Print("Error setting up logging system to ", clicfg["logfile"])
 | 
							log.Print("Error setting up logging system to ", clicfg["logfile"])
 | 
				
			||||||
@@ -187,7 +210,7 @@ func main() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Register interrupt handler
 | 
						// Register interrupt handler
 | 
				
			||||||
	shutdown(&wg, &config, sink, recv)
 | 
						shutdown(&wg, &config, sink, recv, clicfg["pidfile"])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Initialize all collectors
 | 
						// Initialize all collectors
 | 
				
			||||||
	tmp := make([]string, 0)
 | 
						tmp := make([]string, 0)
 | 
				
			||||||
@@ -266,8 +289,8 @@ func main() {
 | 
				
			|||||||
				// Send out node metrics
 | 
									// Send out node metrics
 | 
				
			||||||
				if len(nodeFields) > 0 {
 | 
									if len(nodeFields) > 0 {
 | 
				
			||||||
					nodeTags := map[string]string{"host": host}
 | 
										nodeTags := map[string]string{"host": host}
 | 
				
			||||||
					for _, k := range config.DefTags {
 | 
										for k, v := range config.DefTags {
 | 
				
			||||||
						nodeTags[k] = config.DefTags[k]
 | 
											nodeTags[k] = v
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
					sink.Write("node", nodeTags, nodeFields, t)
 | 
										sink.Write("node", nodeTags, nodeFields, t)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
@@ -277,8 +300,8 @@ func main() {
 | 
				
			|||||||
					for sid, socket := range socketsFields {
 | 
										for sid, socket := range socketsFields {
 | 
				
			||||||
						if len(socket) > 0 {
 | 
											if len(socket) > 0 {
 | 
				
			||||||
							socketTags := map[string]string{"socket": fmt.Sprintf("%d", sid), "host": host}
 | 
												socketTags := map[string]string{"socket": fmt.Sprintf("%d", sid), "host": host}
 | 
				
			||||||
							for _, k := range config.DefTags {
 | 
												for k, v := range config.DefTags {
 | 
				
			||||||
								socketTags[k] = config.DefTags[k]
 | 
													socketTags[k] = v
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
							sink.Write("socket", socketTags, socket, t)
 | 
												sink.Write("socket", socketTags, socket, t)
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
@@ -290,8 +313,8 @@ func main() {
 | 
				
			|||||||
					for cid, cpu := range cpuFields {
 | 
										for cid, cpu := range cpuFields {
 | 
				
			||||||
						if len(cpu) > 0 {
 | 
											if len(cpu) > 0 {
 | 
				
			||||||
							cpuTags := map[string]string{"cpu": fmt.Sprintf("%d", cid), "host": host}
 | 
												cpuTags := map[string]string{"cpu": fmt.Sprintf("%d", cid), "host": host}
 | 
				
			||||||
							for _, k := range config.DefTags {
 | 
												for k, v := range config.DefTags {
 | 
				
			||||||
								cpuTags[k] = config.DefTags[k]
 | 
													cpuTags[k] = v
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
							sink.Write("cpu", cpuTags, cpu, t)
 | 
												sink.Write("cpu", cpuTags, cpu, t)
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user