Commit b66d55f6 authored by Giuseppe Attardi's avatar Giuseppe Attardi
Browse files

Removed layer/tmp*

parent 7121811e
# layer-debug
Juju base layer to collect debug information.
## What does it do?
This base layer provides a `debug` action, which can be used to collect debug
information of a live unit:
```
$ juju run-action debug-test/0 debug
Action queued with id: 4b26e339-7366-4dc7-80ed-255ac0377020`
```
This produces a .tar.gz file which you can retrieve:
```
$ juju show-action-output 4b26e339-7366-4dc7-80ed-255ac0377020
results:
command: juju scp debug-test/0:/home/ubuntu/debug-20161110151539.tar.gz .
message: Archive has been created on unit debug-test/0. Use the juju scp
command to copy it to your local machine.
path: /home/ubuntu/debug-20161110151539.tar.gz
status: completed
timing:
completed: 2016-11-10 15:15:41 +0000 UTC
enqueued: 2016-11-10 15:15:38 +0000 UTC
started: 2016-11-10 15:15:40 +0000 UTC
$ juju scp debug-test/0:/home/ubuntu/debug-20161110151539.tar.gz .
```
The archive includes basic information such as systemctl status, Juju logs,
charm unit data, etc. Additional application-specific information may be
included as well.
## How do I include it?
To include the `debug` action in your charm, simply add it to the includes
section of your `layer.yaml`:
```
includes:
- layer:debug
```
This provides the `debug` action with basic information. That's it!
## Adding application-specific information
When the `debug` action is run, all executable files in the charm's
`debug-scripts/` folder are run, and their output is included in the archive.
You can include application-specific information by creating this folder and
adding your own scripts to it.
> Careful: Make sure script names are unique. If two different layers have a
> script with the same name, and both are included in a charm, then one script
> will override the other.
Both stdout and stderr of debug scripts are captured. Alternatively, the
`DEBUG_SCRIPT_DIR` environment variable points to a directory that the debug
script can add files to.
A few examples can be seen [here](debug-scripts).
If you're writing a Python script, there is also a minimal `debug_script`
library you can import and use. [Example usage](debug-scripts/charm-unitdata)
debug:
description: Collect debug data
#!/usr/local/sbin/charm-env python3
import os
import subprocess
import tarfile
import tempfile
import traceback
from contextlib import contextmanager
from datetime import datetime
from charmhelpers.core.hookenv import action_set, local_unit
archive_dir = None
log_file = None
@contextmanager
def archive_context():
""" Open a context with a new temporary directory.
When the context closes, the directory is archived, and the archive
location is added to Juju action output. """
global archive_dir
global log_file
with tempfile.TemporaryDirectory() as temp_dir:
name = "debug-" + datetime.now().strftime("%Y%m%d%H%M%S")
archive_dir = os.path.join(temp_dir, name)
os.makedirs(archive_dir)
with open("%s/debug.log" % archive_dir, "w") as log_file:
yield
os.chdir(temp_dir)
tar_path = "/home/ubuntu/%s.tar.gz" % name
with tarfile.open(tar_path, "w:gz") as f:
f.add(name)
action_set({
"path": tar_path,
"command": "juju scp %s:%s ." % (local_unit(), tar_path),
"message": " ".join([
"Archive has been created on unit %s." % local_unit(),
"Use the juju scp command to copy it to your local machine."
])
})
def log(msg):
""" Log a message that will be included in the debug archive.
Must be run within archive_context """
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
for line in str(msg).splitlines():
log_file.write(timestamp + " | " + line.rstrip() + "\n")
def run_script(script):
""" Run a single script. Must be run within archive_context """
log("Running script: " + script)
script_dir = os.path.join(archive_dir, script)
os.makedirs(script_dir)
env = os.environ.copy()
env["PYTHONPATH"] = "lib" # allow same imports as reactive code
env["DEBUG_SCRIPT_DIR"] = script_dir
with open(script_dir + "/stdout", "w") as stdout:
with open(script_dir + "/stderr", "w") as stderr:
process = subprocess.Popen(
"debug-scripts/" + script,
stdout=stdout, stderr=stderr, env=env
)
exit_code = process.wait()
if exit_code != 0:
log("ERROR: %s failed with exit code %d" % (script, exit_code))
def run_all_scripts():
""" Run all scripts. For the sake of robustness, log and ignore any
exceptions that occur.
Must be run within archive_context """
scripts = os.listdir("debug-scripts")
for script in scripts:
try:
run_script(script)
except:
log(traceback.format_exc())
def main():
""" Open an archive context and run all scripts. """
with archive_context():
run_all_scripts()
if __name__ == "__main__":
main()
#!/usr/local/sbin/charm-env python3
import debug_script
import json
from charmhelpers.core import unitdata
kv = unitdata.kv()
data = kv.getrange("")
with debug_script.open_file("unitdata.json", "w") as f:
json.dump(data, f, indent=2)
f.write("\n")
#!/bin/sh
set -ux
# report file system disk space usage
df -hT > $DEBUG_SCRIPT_DIR/df-hT
# estimate file space usage
du -h / 2>&1 > $DEBUG_SCRIPT_DIR/du-h
# list the mounted filesystems
mount > $DEBUG_SCRIPT_DIR/mount
# list the mounted systems with ascii trees
findmnt -A > $DEBUG_SCRIPT_DIR/findmnt
# list block devices
lsblk > $DEBUG_SCRIPT_DIR/lsblk
# list open files
lsof 2>&1 > $DEBUG_SCRIPT_DIR/lsof
# list local system locks
lslocks > $DEBUG_SCRIPT_DIR/lslocks
#!/bin/sh
set -ux
cp -v /var/log/juju/* $DEBUG_SCRIPT_DIR
#!/bin/sh
set -ux
ifconfig -a > $DEBUG_SCRIPT_DIR/ifconfig
cp -v /etc/resolv.conf $DEBUG_SCRIPT_DIR/resolv.conf
cp -v /etc/network/interfaces $DEBUG_SCRIPT_DIR/interfaces
netstat -planut > $DEBUG_SCRIPT_DIR/netstat
route -n > $DEBUG_SCRIPT_DIR/route
iptables-save > $DEBUG_SCRIPT_DIR/iptables-save
dig google.com > $DEBUG_SCRIPT_DIR/dig-google
ping -w 2 -i 0.1 google.com > $DEBUG_SCRIPT_DIR/ping-google
#!/bin/sh
set -ux
dpkg --list > $DEBUG_SCRIPT_DIR/dpkg-list
snap list > $DEBUG_SCRIPT_DIR/snap-list
pip2 list > $DEBUG_SCRIPT_DIR/pip2-list
pip3 list > $DEBUG_SCRIPT_DIR/pip3-list
#!/bin/sh
set -ux
sysctl -a > $DEBUG_SCRIPT_DIR/sysctl
#!/bin/sh
set -ux
systemctl --all > $DEBUG_SCRIPT_DIR/systemctl
journalctl > $DEBUG_SCRIPT_DIR/journalctl
systemd-analyze time > $DEBUG_SCRIPT_DIR/systemd-analyze-time
systemd-analyze blame > $DEBUG_SCRIPT_DIR/systemd-analyze-blame
systemd-analyze critical-chain > $DEBUG_SCRIPT_DIR/systemd-analyze-critical-chain
systemd-analyze dump > $DEBUG_SCRIPT_DIR/systemd-analyze-dump
import os
dir = os.environ["DEBUG_SCRIPT_DIR"]
def open_file(path, *args, **kwargs):
""" Open a file within the debug script dir """
return open(os.path.join(dir, path), *args, **kwargs)
name: debug
summary: Provides a troubleshooting debug action
maintainers:
- George Kraft <george.kraft@canonical.com>
description: |
This layer provides a debug action, which allows users to gather
charm-specific debug data for troubleshooting purposes.
tags:
- misc
subordinate: false
# layer-debug
Juju base layer to collect debug information.
## What does it do?
This base layer provides a `debug` action, which can be used to collect debug
information of a live unit:
```
$ juju run-action debug-test/0 debug
Action queued with id: 4b26e339-7366-4dc7-80ed-255ac0377020`
```
This produces a .tar.gz file which you can retrieve:
```
$ juju show-action-output 4b26e339-7366-4dc7-80ed-255ac0377020
results:
command: juju scp debug-test/0:/home/ubuntu/debug-20161110151539.tar.gz .
message: Archive has been created on unit debug-test/0. Use the juju scp
command to copy it to your local machine.
path: /home/ubuntu/debug-20161110151539.tar.gz
status: completed
timing:
completed: 2016-11-10 15:15:41 +0000 UTC
enqueued: 2016-11-10 15:15:38 +0000 UTC
started: 2016-11-10 15:15:40 +0000 UTC
$ juju scp debug-test/0:/home/ubuntu/debug-20161110151539.tar.gz .
```
The archive includes basic information such as systemctl status, Juju logs,
charm unit data, etc. Additional application-specific information may be
included as well.
## How do I include it?
To include the `debug` action in your charm, simply add it to the includes
section of your `layer.yaml`:
```
includes:
- layer:debug
```
This provides the `debug` action with basic information. That's it!
## Adding application-specific information
When the `debug` action is run, all executable files in the charm's
`debug-scripts/` folder are run, and their output is included in the archive.
You can include application-specific information by creating this folder and
adding your own scripts to it.
> Careful: Make sure script names are unique. If two different layers have a
> script with the same name, and both are included in a charm, then one script
> will override the other.
Both stdout and stderr of debug scripts are captured. Alternatively, the
`DEBUG_SCRIPT_DIR` environment variable points to a directory that the debug
script can add files to.
A few examples can be seen [here](debug-scripts).
If you're writing a Python script, there is also a minimal `debug_script`
library you can import and use. [Example usage](debug-scripts/charm-unitdata)
debug:
description: Collect debug data
#!/usr/local/sbin/charm-env python3
import os
import subprocess
import tarfile
import tempfile
import traceback
from contextlib import contextmanager
from datetime import datetime
from charmhelpers.core.hookenv import action_set, local_unit
archive_dir = None
log_file = None
@contextmanager
def archive_context():
""" Open a context with a new temporary directory.
When the context closes, the directory is archived, and the archive
location is added to Juju action output. """
global archive_dir
global log_file
with tempfile.TemporaryDirectory() as temp_dir:
name = "debug-" + datetime.now().strftime("%Y%m%d%H%M%S")
archive_dir = os.path.join(temp_dir, name)
os.makedirs(archive_dir)
with open("%s/debug.log" % archive_dir, "w") as log_file:
yield
os.chdir(temp_dir)
tar_path = "/home/ubuntu/%s.tar.gz" % name
with tarfile.open(tar_path, "w:gz") as f:
f.add(name)
action_set({
"path": tar_path,
"command": "juju scp %s:%s ." % (local_unit(), tar_path),
"message": " ".join([
"Archive has been created on unit %s." % local_unit(),
"Use the juju scp command to copy it to your local machine."
])
})
def log(msg):
""" Log a message that will be included in the debug archive.
Must be run within archive_context """
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
for line in str(msg).splitlines():
log_file.write(timestamp + " | " + line.rstrip() + "\n")
def run_script(script):
""" Run a single script. Must be run within archive_context """
log("Running script: " + script)
script_dir = os.path.join(archive_dir, script)
os.makedirs(script_dir)
env = os.environ.copy()
env["PYTHONPATH"] = "lib" # allow same imports as reactive code
env["DEBUG_SCRIPT_DIR"] = script_dir
with open(script_dir + "/stdout", "w") as stdout:
with open(script_dir + "/stderr", "w") as stderr:
process = subprocess.Popen(
"debug-scripts/" + script,
stdout=stdout, stderr=stderr, env=env
)
exit_code = process.wait()
if exit_code != 0:
log("ERROR: %s failed with exit code %d" % (script, exit_code))
def run_all_scripts():
""" Run all scripts. For the sake of robustness, log and ignore any
exceptions that occur.
Must be run within archive_context """
scripts = os.listdir("debug-scripts")
for script in scripts:
try:
run_script(script)
except:
log(traceback.format_exc())
def main():
""" Open an archive context and run all scripts. """
with archive_context():
run_all_scripts()
if __name__ == "__main__":
main()
#!/usr/local/sbin/charm-env python3
import debug_script
import json
from charmhelpers.core import unitdata
kv = unitdata.kv()
data = kv.getrange("")
with debug_script.open_file("unitdata.json", "w") as f:
json.dump(data, f, indent=2)
f.write("\n")
#!/bin/sh
set -ux
# report file system disk space usage
df -hT > $DEBUG_SCRIPT_DIR/df-hT
# estimate file space usage
du -h / 2>&1 > $DEBUG_SCRIPT_DIR/du-h
# list the mounted filesystems
mount > $DEBUG_SCRIPT_DIR/mount
# list the mounted systems with ascii trees
findmnt -A > $DEBUG_SCRIPT_DIR/findmnt
# list block devices
lsblk > $DEBUG_SCRIPT_DIR/lsblk
# list open files
lsof 2>&1 > $DEBUG_SCRIPT_DIR/lsof
# list local system locks
lslocks > $DEBUG_SCRIPT_DIR/lslocks
#!/bin/sh
set -ux
cp -v /var/log/juju/* $DEBUG_SCRIPT_DIR
#!/bin/sh
set -ux
ifconfig -a > $DEBUG_SCRIPT_DIR/ifconfig
cp -v /etc/resolv.conf $DEBUG_SCRIPT_DIR/resolv.conf
cp -v /etc/network/interfaces $DEBUG_SCRIPT_DIR/interfaces
netstat -planut > $DEBUG_SCRIPT_DIR/netstat
route -n > $DEBUG_SCRIPT_DIR/route
iptables-save > $DEBUG_SCRIPT_DIR/iptables-save
dig google.com > $DEBUG_SCRIPT_DIR/dig-google
ping -w 2 -i 0.1 google.com > $DEBUG_SCRIPT_DIR/ping-google
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment