2019-10-31 14:32:46 -04:00
// Copyright (C) 2018 spdfg
2019-11-20 13:33:46 -05:00
//
2018-10-06 20:03:14 -07:00
// This file is part of Elektron.
2019-11-20 13:33:46 -05:00
//
2018-10-06 20:03:14 -07:00
// Elektron is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2019-11-20 13:33:46 -05:00
//
2018-10-06 20:03:14 -07:00
// Elektron is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
2019-11-20 13:33:46 -05:00
//
2018-10-06 20:03:14 -07:00
// You should have received a copy of the GNU General Public License
// along with Elektron. If not, see <http://www.gnu.org/licenses/>.
2019-11-20 13:33:46 -05:00
//
2018-10-06 20:03:14 -07:00
2016-09-26 19:14:51 -04:00
package pcp
2016-09-22 18:34:05 -04:00
import (
2016-09-22 19:54:06 -04:00
"bufio"
2016-10-13 17:15:09 -04:00
"os/exec"
2016-10-07 20:47:59 -04:00
"syscall"
2016-10-13 17:15:09 -04:00
"time"
2018-09-30 18:23:38 -07:00
2019-11-21 14:12:53 -05:00
log "github.com/sirupsen/logrus"
2019-12-05 17:38:56 -05:00
elekLog "github.com/spdfg/elektron/logging"
2019-12-05 21:43:16 -05:00
. "github.com/spdfg/elektron/logging/types"
2016-09-22 18:34:05 -04:00
)
2019-11-20 13:33:46 -05:00
func Start ( quit chan struct { } , logging * bool , pcpConfigFile string ) {
2018-10-04 19:21:45 -04:00
var pcpCommand string = "pmdumptext -m -l -f '' -t 1.0 -d , -c " + pcpConfigFile
cmd := exec . Command ( "sh" , "-c" , pcpCommand )
2016-10-07 20:47:59 -04:00
cmd . SysProcAttr = & syscall . SysProcAttr { Setpgid : true }
2016-09-22 18:34:05 -04:00
2016-09-26 19:14:51 -04:00
pipe , err := cmd . StdoutPipe ( )
if err != nil {
2019-11-21 14:12:53 -05:00
log . Fatal ( err )
2016-09-26 19:14:51 -04:00
}
2016-09-22 18:34:05 -04:00
//cmd.Stdout = stdout
scanner := bufio . NewScanner ( pipe )
2016-09-26 19:14:51 -04:00
go func ( logging * bool ) {
2017-09-28 15:36:47 -04:00
// Get names of the columns.
2016-09-22 18:34:05 -04:00
scanner . Scan ( )
2018-01-19 21:20:43 +00:00
// Write to logfile
2019-12-05 21:43:16 -05:00
elekLog . Log ( PCP , log . InfoLevel , scanner . Text ( ) )
2016-09-26 19:14:51 -04:00
2018-02-03 19:48:17 -05:00
// Throw away first set of results
2016-09-22 18:34:05 -04:00
scanner . Scan ( )
seconds := 0
2018-02-02 19:24:51 -05:00
2016-09-22 18:34:05 -04:00
for scanner . Scan ( ) {
2018-02-02 19:24:51 -05:00
text := scanner . Text ( )
2016-09-26 19:14:51 -04:00
2016-10-13 17:15:09 -04:00
if * logging {
2019-12-05 21:43:16 -05:00
elekLog . Log ( PCP , log . InfoLevel , text )
2016-09-26 19:14:51 -04:00
}
2016-09-22 18:34:05 -04:00
seconds ++
}
2016-09-26 19:14:51 -04:00
} ( logging )
2019-12-05 21:43:16 -05:00
elekLog . Log ( CONSOLE , log . InfoLevel , "PCP logging started" )
2016-09-22 18:34:05 -04:00
if err := cmd . Start ( ) ; err != nil {
2019-11-21 14:12:53 -05:00
log . Fatal ( err )
2016-09-22 18:34:05 -04:00
}
2016-09-26 19:14:51 -04:00
2016-10-07 20:47:59 -04:00
pgid , err := syscall . Getpgid ( cmd . Process . Pid )
2016-10-13 17:15:09 -04:00
select {
case <- quit :
2019-12-05 21:43:16 -05:00
elekLog . Log ( CONSOLE , log . InfoLevel , "Stopping PCP logging in 5 seconds" )
2016-09-26 19:14:51 -04:00
time . Sleep ( 5 * time . Second )
2016-10-07 20:47:59 -04:00
// http://stackoverflow.com/questions/22470193/why-wont-go-kill-a-child-process-correctly
2017-09-28 15:36:47 -04:00
// Kill process and all children processes.
2016-10-07 20:47:59 -04:00
syscall . Kill ( - pgid , 15 )
2016-09-26 19:14:51 -04:00
return
}
2016-09-22 18:34:05 -04:00
}