| 
									
										
										
										
											2018-05-18 20:47:54 +00:00
										 |  |  | package glutton | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-10-15 14:50:39 +00:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2018-05-18 20:47:54 +00:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2024-03-05 18:50:35 +00:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2018-05-18 20:47:54 +00:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"runtime" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2024-03-05 18:50:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/glaslos/lsof" | 
					
						
							|  |  |  | 	"github.com/google/gopacket/pcap" | 
					
						
							| 
									
										
										
										
											2018-05-18 20:47:54 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 14:50:39 +00:00
										 |  |  | func countOpenFiles() (int, error) { | 
					
						
							|  |  |  | 	if runtime.GOOS == "linux" { | 
					
						
							| 
									
										
										
										
											2024-03-05 18:50:35 +00:00
										 |  |  | 		lines, err := lsof.ReadPID(os.Getpid()) | 
					
						
							|  |  |  | 		return len(lines) - 1, err | 
					
						
							| 
									
										
										
										
											2018-05-18 20:47:54 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-03-05 18:50:35 +00:00
										 |  |  | 	return 0, errors.New("operating system type not supported for this command") | 
					
						
							| 
									
										
										
										
											2018-05-18 20:47:54 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (g *Glutton) startMonitor(quit chan struct{}) { | 
					
						
							|  |  |  | 	ticker := time.NewTicker(10 * time.Second) | 
					
						
							|  |  |  | 	go func() { | 
					
						
							|  |  |  | 		for { | 
					
						
							|  |  |  | 			select { | 
					
						
							| 
									
										
										
										
											2024-03-05 18:50:35 +00:00
										 |  |  | 			// case <-ticker.C:
 | 
					
						
							| 
									
										
										
										
											2024-11-28 11:03:57 +00:00
										 |  |  | 			// 	openFiles, err := countOpenFiles()
 | 
					
						
							|  |  |  | 			// 	if err != nil {
 | 
					
						
							|  |  |  | 			// 		fmt.Printf("Failed :%s", err)
 | 
					
						
							|  |  |  | 			// 	}
 | 
					
						
							|  |  |  | 			// 	runningRoutines := runtime.NumGoroutine()
 | 
					
						
							|  |  |  | 			// 	g.Logger.Info(fmt.Sprintf("running Go routines: %d, open files: %d", openFiles, runningRoutines))
 | 
					
						
							| 
									
										
										
										
											2018-05-18 20:47:54 +00:00
										 |  |  | 			case <-quit: | 
					
						
							| 
									
										
										
										
											2024-03-05 18:50:35 +00:00
										 |  |  | 				g.Logger.Info("monitoring stopped...") | 
					
						
							| 
									
										
										
										
											2018-05-18 20:47:54 +00:00
										 |  |  | 				ticker.Stop() | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-15 14:50:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-05 18:50:35 +00:00
										 |  |  | func getNonLoopbackIPs(ifaceName string) ([]net.IP, error) { | 
					
						
							|  |  |  | 	nonLoopback := []net.IP{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ifs, err := pcap.FindAllDevs() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nonLoopback, err | 
					
						
							| 
									
										
										
										
											2019-10-15 14:50:39 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-03-05 18:50:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for _, iface := range ifs { | 
					
						
							|  |  |  | 		if strings.EqualFold(iface.Name, ifaceName) { | 
					
						
							|  |  |  | 			for _, addr := range iface.Addresses { | 
					
						
							|  |  |  | 				if !addr.IP.IsLoopback() && addr.IP.To4() != nil { | 
					
						
							|  |  |  | 					nonLoopback = append(nonLoopback, addr.IP) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(nonLoopback) == 0 { | 
					
						
							|  |  |  | 		return nonLoopback, fmt.Errorf("unable to find any non-loopback addresses for: %s", ifaceName) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nonLoopback, nil | 
					
						
							| 
									
										
										
										
											2019-10-15 14:50:39 +00:00
										 |  |  | } |