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-09-22 18:34:05 -04:00
"log"
2016-09-26 19:14:51 -04:00
"os"
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"
2016-09-22 18:34:05 -04:00
)
2016-10-07 19:29:36 -04:00
func Start ( quit chan struct { } , logging * bool , prefix string ) {
2016-09-26 19:14:51 -04:00
const pcpCommand string = "pmdumptext -m -l -f '' -t 1.0 -d , -c config"
2016-09-22 18:34:05 -04:00
cmd := exec . Command ( "sh" , "-c" , pcpCommand )
2016-10-07 20:47:59 -04:00
cmd . SysProcAttr = & syscall . SysProcAttr { Setpgid : true }
2016-09-26 19:14:51 -04:00
startTime := time . Now ( ) . Format ( "20060102150405" )
2016-09-22 18:34:05 -04:00
2016-10-13 17:15:09 -04:00
logFile , err := os . Create ( "./" + prefix + startTime + ".pcplog" )
2016-09-26 19:14:51 -04:00
if err != nil {
log . Fatal ( err )
}
2016-11-14 22:40:31 -05:00
log . Println ( "Writing pcp logs to file: " + logFile . Name ( ) )
2016-09-26 19:14:51 -04:00
defer logFile . Close ( )
pipe , err := cmd . StdoutPipe ( )
if err != nil {
log . Fatal ( err )
}
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 ) {
2016-09-22 18:34:05 -04:00
// Get names of the columns
scanner . Scan ( )
2016-09-26 19:14:51 -04:00
// Write to logfile
logFile . WriteString ( scanner . Text ( ) + "\n" )
/ *
2016-10-13 17:15:09 -04:00
headers := strings . Split ( scanner . Text ( ) , "," )
2016-09-22 18:34:05 -04:00
2016-10-13 17:15:09 -04:00
for _ , hostMetric := range headers {
split := strings . Split ( hostMetric , ":" )
fmt . Printf ( "Host %s: Metric: %s\n" , split [ 0 ] , split [ 1 ] )
}
2016-09-26 19:14:51 -04:00
* /
2016-09-22 18:34:05 -04:00
// Throw away first set of results
scanner . Scan ( )
seconds := 0
for scanner . Scan ( ) {
2016-09-26 19:14:51 -04:00
2016-10-13 17:15:09 -04:00
if * logging {
2016-09-26 19:14:51 -04:00
log . Println ( "Logging PCP..." )
logFile . WriteString ( scanner . Text ( ) + "\n" )
}
/ *
2016-10-13 17:15:09 -04:00
fmt . Printf ( "Second: %d\n" , seconds )
for i , val := range strings . Split ( scanner . Text ( ) , "," ) {
fmt . Printf ( "host metric: %s val: %s\n" , headers [ i ] , val )
} * /
2016-09-22 19:54:06 -04:00
2016-09-22 18:34:05 -04:00
seconds ++
2016-09-22 19:54:06 -04:00
2016-09-26 19:14:51 -04:00
// fmt.Println("--------------------------------")
2016-09-22 18:34:05 -04:00
}
2016-09-26 19:14:51 -04:00
} ( logging )
log . Println ( "PCP logging started" )
2016-09-22 18:34:05 -04:00
if err := cmd . Start ( ) ; err != nil {
log . Fatal ( err )
}
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 :
2016-09-26 19:14:51 -04:00
log . Println ( "Stopping PCP logging in 5 seconds" )
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
// kill process and all children processes
syscall . Kill ( - pgid , 15 )
2016-09-26 19:14:51 -04:00
return
}
2016-09-22 18:34:05 -04:00
}