func (file *FileReader) Run(dest chan<- Message) { var err error file.watcher, err = fsnotify.NewWatcher() if err != nil { file.errchan <- &InputError{file.Driver, file.Id, "Watcher " + file.Source, err} return } file.watcher.Add(file.Source) data := make(chan string, 100) defer file.Close() go reader_to_channel(file, data) for { select { case line := <-data: dest <- packmsg(file.Id, syslog5424.CreateMessage(file.AppName, *file.prio, line)) case <-file.end: return } } }
func (intl *InternalReport) Run(dest chan<- Message) { memStats := new(runtime.MemStats) ticker := time.Tick(intl.Tick.Get().(time.Duration)) pid := strconv.Itoa(os.Getpid()) for { select { case <-ticker: runtime.ReadMemStats(memStats) stat := statistic_report{ Tasks: uint(runtime.NumGoroutine()), MemAllocated: uint(memStats.Alloc), SysAlloc: uint(memStats.Sys), Heap: uint(memStats.HeapAlloc), Stack: uint(memStats.StackInuse), } dest <- packmsg(intl.Id, syslog5424.CreateMessage( intl.AppName, syslog5424.LOG_SYSLOG|syslog5424.LOG_INFO, stringify_statistics(stat)).ProcID(pid).MsgID("statistics")) case <-intl.end: return } } }
func (fifo *FIFOReader) Run(dest chan<- Message) { var err error syscall.Mkfifo(fifo.Source, 0644) fifo.fi_des, err = os.OpenFile(fifo.Source, os.O_RDONLY, 0644) if err != nil { fifo.errchan <- &InputError{fifo.Driver, fifo.Id, "FIFO " + fifo.Source, err} return } data := make(chan string, 100) defer fifo.fi_des.Close() go reader_to_channel(fifo.fi_des, data) for { select { case line := <-data: dest <- packmsg(fifo.Id, syslog5424.CreateMessage(fifo.AppName, *fifo.prio, line)) case <-fifo.end: return } } }
func (mc *mercury_conf) MainLoop() { sd_notify("READY", "1") mc.Log(syslog5424.LOG_NOTICE, "start", "drivers available="+strconv.Itoa(len(mc.drivers))) log.SetFlags(log.Ltime | log.Lshortfile) pid := strconv.Itoa(os.Getpid()) for { select { case logmsg := <-mc.internalchan: msg := syslog5424.CreateMessage(MERCURYNAME, syslog5424.LOG_SYSLOG|logmsg.Severity, logmsg.Message).ProcID(pid) if logmsg.Category != "" && logmsg.Category != "-" { msg = msg.MsgID(logmsg.Category) } s_msg := msg.String() for _, v := range mc.outputs { if out, ok := mc.instances[v]; ok { if _, ok := out.(Output); ok { out.(Output).Send(s_msg) } } } case datum := <-mc.inputchan: msg := datum.Message.String() // TODO defensive cast to avoid panic if _, ok := mc.instances[datum.Source].(Input); !ok { mc.Log(syslog5424.LOG_ERR, "dispatcher", fmt.Sprintf("[%s] is not INPUT", datum.Source)) continue } outputs := mc.instances[datum.Source].(Input).SendTo() for _, v := range outputs { if out, ok := mc.instances[v]; ok { if _, ok := out.(Output); !ok { mc.Log(syslog5424.LOG_ERR, "dispatcher", fmt.Sprintf("[%s] from [%s] is not OUTPUT", v, datum.Source)) continue } out.(Output).Send(msg) } } case err := <-mc.errchan: mc.Log(syslog5424.LOG_ERR, "plugin", err.Error()) case <-mc.mainend: return } } }
// <29>Dec 4 11:02:35 pdns[2030]: func parse_3164(data []byte) (syslog5424.Message, error) { part := FieldsFuncN(string(data), 10, get_3164tokenizer()) switch len(part) { case 0, 1, 2, 3: return syslog5424.EmptyMessage(), errors.New("wrong format 3164 : " + string(data)) case 4: prio, err := strconv.Atoi(part[0]) if err != nil { return syslog5424.EmptyMessage(), errors.New("Wrong Priority :" + string(part[0])) } return syslog5424.CreateMessage(part[2], syslog5424.Priority(prio), part[3]).Stamp(part[1]), nil default: prio, err := strconv.Atoi(part[0]) if err != nil { return syslog5424.EmptyMessage(), errors.New("Wrong Priority :" + string(part[0])) } return syslog5424.CreateMessage(part[2], syslog5424.Priority(prio), part[4]).Stamp(part[1]).ProcID(part[3]), nil } }
// statefull parser for linux printk() message buffer // // BUG(nath): may need some generic API func ParseMessage_KLog(boot_ts time.Time, data string) (syslog5424.Message, error) { log.SetFlags(log.Ltime | log.Lshortfile) part := FieldsFuncN(data, 4, get_klog_tokenizer()) if len(part) < 4 { log.Println(data) for pi := range part { log.Println(part[pi]) } } switch len(part) { case 2: prio, err := strconv.Atoi(part[0]) if err != nil { return syslog5424.EmptyMessage(), errors.New("Wrong Priority :" + string(part[0])) } return syslog5424.CreateMessage("-", syslog5424.Priority(prio), part[2]), nil case 3: // (kern) 0 * 8 + 6 (info) return syslog5424.CreateMessage("-", syslog5424.Priority(6), part[2]).Delta(boot_ts, part[0], part[1]), nil case 4: prio, err := strconv.Atoi(part[0]) if err != nil { return syslog5424.EmptyMessage(), errors.New("Wrong Priority :" + string(part[0])) } return syslog5424.CreateMessage("-", syslog5424.Priority(prio), part[3]).Delta(boot_ts, part[1], part[2]), nil default: // (kern) 0 * 8 + 6 (info) return syslog5424.CreateMessage("-", syslog5424.Priority(6), data), nil } }