Monitor Linux server with sysdig


SUBMITTED BY: sahertian

DATE: June 21, 2016, 12:57 p.m.

FORMAT: Text only

SIZE: 6.2 kB

HITS: 731

  1. Well strace, tcpdump, and lsof are indeed great utilities that should be part of every sysadmin's toolset, and that is precisely the reason why you will love sysdig, a powerful open source tool for system-level exploration and troubleshooting, introduced by its creators as "strace + tcpdump + lsof + awesome sauce with a little Lua cherry on top." Humor aside, one of the great features of sysdig resides in its ability not only to analyze the "live" state of a Linux system, but also to save the state in a dump file for offline inspection. What's more, you can customize sysdig's behavior or even enhance its capabilities by using built-in (or writing your own) small scripts called chisels. Individual chisels are used to analyze sysdig-captured event streams in various script-specific fashions.
  2. In this tutorial we'll explore the installation and basic usage of sysdig to perform system monitoring and troubleshooting on Linux.
  3. Installing Sysdig
  4. For this tutorial, we will choose to use the automatic installation process described in the official website for the sake of simplicity, brevity, and distribution agnosticity. In the automatic process, the installation script automatically detects the operating system and installs all the necessary dependencies.
  5. Run the following command as root to install sysdig from the official apt/yum repository:
  6. # curl -s https://s3.amazonaws.com/download.draios.com/stable/install-sysdig | bash
  7. Once the installation is complete, we can invoke sysdig as follows to get a feel for it:
  8. # sysdig
  9. Our screen will be immediately filled with all that is going on in our system, not allowing us to do much more with that information. For that reason, we will run:
  10. # sysdig -cl | less
  11. to see a list of available chisels.
  12. The following categories are available by default, each of which is populated by multiple built-in chisels.
  13. CPU Usage
  14. Errors
  15. I/O
  16. Logs
  17. Misc
  18. Net
  19. Performance
  20. Security
  21. System State
  22. To display information (including detailed command-line usage) on a particular chisel, run:
  23. # sysdig -cl [chisel_name]
  24. For example, we can check information about spy_port chisel under "Net" category by running:
  25. # sysdig -i spy_port
  26. Chisels can be combined with filters (which can be applied to both live data or a trace file) to obtain more useful output.
  27. Filters follow a "class.field" structure. For example:
  28. fd.cip: client IP address.
  29. evt.dir: event direction can be either '>' for enter events or '<' for exit events.
  30. The complete filter list can be displayed with:
  31. # sysdig -l
  32. In the rest of the tutorial, I will demonstrate several use cases of sysdig.
  33. Sysdig Example: Troubleshooting Server Performance
  34. Suppose your server is experiencing performance issues (e.g., unresponsiveness or significant delays in responding). You can use the bottlenecks chisel to display a list of the 10 slowest systems calls at the moment.
  35. Use the following command to check up on a live server in real time. The "-c" flag followed by a chisel name tells sysdig to run the specified chisel.
  36. # sysdig -c bottlenecks
  37. Alternatively, you can conduct a server performance analysis offline. In that case, you can save a complete sysdig trace to a file, and run the bottlenecks chisel against the trace as follows.
  38. First, save a sysdig trace (use Ctrl+c to stop the collection):
  39. # sysdig -w trace.scap
  40. Once the trace is collected, you can check the slowest systems calls that were performed during the capture interval by running:
  41. # sysdig -r trace.scap -c bottlenecks
  42. You want to pay attention fo columns #2, #3, and #4, which indicate execution time, process name, and PID, respectively.
  43. Sysdig Example: Monitoring Interactive User Activities
  44. Suppose you as a sysadmin want to monitor interactive user activities in a system (e.g., what command a user typed from the command line, and what directories the user went to). That is when spy_user chisel comes in handy.
  45. Let's first collect a sysdig trace with a couple of extra options.
  46. # sysdig -s 4096 -z -w /mnt/sysdig/$(hostname).scap.gz
  47. "-s 4096" tells sysdig to capture up to 4096 bytes of each event.
  48. "-z" (used with "-w") enables compression for a trace file.
  49. "-w " saves sysdig traces to a specified file.
  50. In the above, we customize the name of the compressed trace file on a per-host basis. Remember that you can interrupt the execution of sysdig at any moment by pressing Ctrl + c.
  51. Once we've collected a reasonable amount of data, we can view interactive activities of every user in a system by running:
  52. # sysdig -r /mnt/sysdig/debian.scap.gz -c spy_users
  53. The first column in the above output indicates the PID of the process associated with a given user's activity.
  54. What if you want to target a specific user, and monitor the user's activities only? You can filter the results of the spy_users chisel by username:
  55. # sysdig -r /mnt/sysdig/debian.scap.gz -c spy_users "user.name=johndoe"
  56. Sysdig Example: Monitoring File I/O
  57. We can customize the output format of sysdig traces with "-p" flag, and indicate desired fields (e.g., user name, process name, and file or socket name) enclosed inside double quotes. In this example, we will create a trace file that will only contain writing events in home directories (which we can inspect later with "sysdig -r writetrace.scap.gz").
  58. # sysdig -p "%user.name %proc.name %fd.name" "evt.type=write and fd.name contains /home/" -z -w writetrace.scap.gz
  59. Sysdig Example: Monitoring Network I/O
  60. As part of server troubleshooting, you may want to snoop on network traffic, which is typically done withtcpdump. With sysdig, traffic sniffing can be done as easily, but in more user friendly fashions.
  61. For example, you can inspect data (in ASCII) that has been exchanged with a particular IP address, served by a particular process (e.g., apache2):
  62. # sysdig -s 4096 -A -c echo_fds fd.cip=192.168.0.100 -r /mnt/sysdig/debian.scap.gz proc.name=apache2
  63. If you want to monitor raw data transfer (in binary) instead, replace "-A" with "-X":
  64. # sysdig -s 4096 -X -c echo_fds fd.cip=192.168.0.100 -r /mnt/sysdig/debian.scap.gz proc.name=apache2
  65. Thank you :)

comments powered by Disqus