Want more features on Pastebin? Sign Up, it's FREE!

nginx to syslog-ng

By: Pryoidain on Jan 24th, 2012  |  syntax: Bash  |  size: 2.84 KB  |  views: 1,898  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ### First let's draft up a quick script to check if pipes exist and create them if needed.
  2.  
  3. ### /scripts/nginxfifo.sh:
  4. #!/bin/bash
  5.  
  6. #
  7. # nginx Syslog-ng Support
  8. # FIFO Creation Script
  9. # DEPENDS: nginx,fifo,syslog-ng
  10. #
  11.  
  12. #
  13. # Check for and Create the Access Log
  14. # FIFO if it doesn't exist.
  15. #
  16. if [ ! -p /var/log/nginx/access.log ]; then
  17.         /bin/rm -f /var/log/nginx/access.log
  18.         /usr/bin/mkfifo --mode=0640 /var/log/nginx/access.log
  19. fi
  20.  
  21. #
  22. # Check for and create the Error log
  23. # FIFO if it doesn't exist.
  24. #
  25. if [ ! -p /var/log/nginx/error.log ] ; then
  26.         /bin/rm -f /var/log/nginx/error.log
  27.         /usr/bin/mkfifo --mode=0640 /var/log/nginx/error.log
  28. fi
  29.  
  30. #
  31. # Change the Ownership and Permissions
  32. #
  33. /bin/chown nginx:root /var/log/nginx/access.log
  34. /bin/chown nginx:root /var/log/nginx/error.log
  35.  
  36. ###
  37. ### Now we need to configure syslog-ng to read from the source.
  38. ### In my case I wanted those messages dropped into a log file so that I could run
  39. ### logcheck on them as well as archive them with logrotate.
  40. ###
  41.  
  42. ### /etc/syslog-ng/syslog-ng.conf
  43. source nginx_access { pipe("/var/log/nginx/access.log" program_override("nginx-access-log: ")); };
  44. source nginx_error { pipe("/var/log/nginx/error.log" program_override("nginx-error-log: ")); };
  45. destination nginxaccess { file("/var/log/syslog-ng/nginx.access.log"); };
  46. destination nginxerror { file("/var/log/syslog-ng/nginx.error.log"); };
  47. filter f_naccess { program("nginx-access-log: "); };
  48. filter f_nerror { program("nginx-error-log: "); };
  49. log { source(nginx_access); filter(f_naccess); destination(nginxaccess); };
  50. log { source(nginx_error); filter(f_nerror); destination(nginxerror); };
  51.  
  52. ###
  53. ### Now, Syslog-ng will flip a shit if it tries to activate itself and the pipes don't exist.
  54. ### So what we need to do is make nginx dependent on syslog-ng, so that syslog-ng starts first
  55. ### and we don't get a queue of logs that will dump in (in the event of an attack even the short
  56. ### time between nginx init and syslog-ng init may result in a very large queue which would be
  57. ### very very bad), and then inside syslog-ng's startup system we need to call the fifo creation
  58. ### script BEFORE we call the service start for syslog-ng.
  59. ###
  60. ### This way, the order goes mkfifo->syslog-ng->nginx.
  61. ###
  62.  
  63. ###
  64. ### First nginx dependencies. This is kinda gentoo centric.
  65. ###
  66.  
  67. ### /etc/init.d/nginx
  68. depend() {
  69.         need net
  70.         need syslog-ng
  71.         use dns logger netmount
  72. }
  73.  
  74. ###
  75. ### Now we modify syslog-ng
  76. ###
  77.  
  78. ### /etc/init.d/syslog-ng
  79. start() {
  80.         checkconfig || return 1
  81.         ebegin "Creating nginx FIFO Pipe"
  82.         /scripts/nginxfifo.sh
  83.         eend $? "Failed to create nginx FIFO Pipe"
  84.         ebegin "Starting syslog-ng"
  85.         ...
  86. }
  87.  
  88. ### Then call syslog-ng to reboot which will take down nginx with it and set everything up.
  89. ### Done and done.
clone this paste RAW Paste Data