-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathdumpthreads.go
More file actions
74 lines (67 loc) · 2.12 KB
/
dumpthreads.go
File metadata and controls
74 lines (67 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// Copyright (c) 2015 Jon Carlson. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
//
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
templ "text/template"
)
// This script creates an SSH session and remotely runs a bash script
var dumpThreadsScript = `
#!/bin/bash
# Remotely dumps Java threads a number of times
ssh -o StrictHostKeyChecking=no {{.host}} 'bash -s' <<-END
#!/bin/bash
COUNT={{.dumpCount}}
# Write the thread dumps to a particular location
FILE="/home/{{.pidOwner}}/logs/threads.\$(date +%Y-%m-%d_%H%M%S).{{.host}}"
PID=\$(ps aux | grep -P '(central|blue).*java' | grep -v grep | grep -v flock | egrep -v 'su (central|blue)' | awk '{print \$2}')
#echo "\$FILE"
#echo "\$PID"
for (( c=1; c<=COUNT; c++ )) ; do
sudo su {{.pidOwner}} -- -c "touch \${FILE}; jstack -l \$PID >> \${FILE}"
echo "Threads dumped... to \$FILE. Sleeping for {{.intervalSeconds}} seconds..."
sleep {{.intervalSeconds}}
done
echo done
END
`
// main generates a script to dumps the Java threads on the given host for the given number of times
// This will need modification for monitoring a Windows-based resource because it creates a
// bash script that is executed remotely.
func main(host, user string, dumpCount int, intervalSeconds int) error {
// Save script file
filename := host + "_dumpThreadScript.sh"
file, err := os.Create(filename)
if err != nil {
return err
}
fmt.Println(" Writing script to file : " + filename)
t := templ.New("Dump threads script")
templ.Must(t.Parse(dumpThreadsScript))
context := map[string]string{
"pidOwner": user, // user is the account process is running as
"dumpCount": strconv.Itoa(dumpCount),
"intervalSeconds": strconv.Itoa(intervalSeconds),
"host": host}
err = t.Execute(file, context)
if err != nil {
return err
}
file.Close()
cmdStr := "./" + host + "_dumpThreadScript.sh"
log.Printf("Executing: %s \n", cmdStr)
sshCmd := exec.Command(cmdStr)
bytes, err := sshCmd.CombinedOutput()
fmt.Printf("Output:\n %s \n", string(bytes))
if err != nil {
return err
}
return nil
}