Log compression Bash script

In my experience as a Systems Administrator, it has come up quite often to create a script to rotate and compress rather large log files.

These log files could be anything: java logs, apache logs (apache should have its own log rotation built in) and mail logs for example. This script has two modes : daily and monthly.

The daily mode is intended to be run daily (obviously) , gzipping the previous days log file. The monthly mode, run monthly (obviously), then tar’s up all the previous month’s gzip files into one big tarball.

Note that this script assumes the log filenames are assorted by the filename + date (year/month/day). This can obviously be modified to suit the specific syntax of your log file names.

Here is the script :

I simply make two crontab entries :

The above entries run the script daily at 3:00am, and monthly on the 1st of every month at 5:00am, this ensures the script isn’t run at the same time on the 1st as the daily job.

That’s it!

Software RAID in Linux

Several occasions have arisen where a client requested software raid-1 between two IDE drives in their server.

Obviously the servers in question had no hardware raid capabilities, and compromising disk I/O read/write times for increased redundancy was more important.

Below is a simple tutorial for setting up software raid in Linux, using MDADM. The specific examples in these instructions are with Debian, but can be applied to any linux distribution.

Linux Software RAID-1

1. Verify that you are working with two identical hard drives:

2. Copy the partition table over to the second drive:

Edit the partition table of the second drive so that all of the partitions, except #3, have type ‘fd’.

3. Install mdadm to manage the arrays.

It’ll ask you a series of questions that are highly dependent on your needs. One key one is: “Yes, automatically start RAID arrays”

4. Load the RAID1 module:

5. Create the RAID1 volumes. Note that we’re setting one mirror as “missing” here. We’ll add the second half of the mirror later because we’re using it right now.

6. Make the filesystems:

7. Install the dump package:

8. Mount the new volumes, dump & restore from the running copies:

9. Set up the chroot environment:

10. Edit /boot/silo.conf, and change the following line:

11. Edit /etc/fstab, and point them to the MD devices:

12. Save the MD information to /etc/mdadm/mdadm.conf:

13. Rebuild the initrd (to add the RAID modules, and boot/root RAID startup information):

14. Leave the chroot environment:

15. Unmount /boot. klogd uses the System.map file, and we need to kill it to unmount /boot.

16. Add /dev/hda1 to /dev/md0 — the /boot mirror

Wait until the mirror is complete. CTRL-C to exit watch.

17. Mount the mirrored /boot:

18. Stamp the boot loader onto both disks, and reboot:

19. Assuming it booted up correctly, verify that we’re running on the mirrored copies:

If so, add the other partitions into their respective mirrors:

And wait until the the mirrors are done building.

20. Edit /etc/mdadm/mdadm.conf and remove any references to the RAID volumes. Refresh the mdadm.conf information:

21. Rebuild the initrd one more time. The previous time only included one half of each mirror for root and swap.

22. Reboot one more time for good measure. You now have software RAID1.

Testing the Software Raid & simulating a drive failure

Newer versions of raidtools come with a raidsetfaulty command. By using raidsetfaulty you can just simulate a drive failure without unplugging things off.

Just running the command

First, you should see something like the first line of this on your system’s log. Something like the second line will appear if you have spare disks configured.

Checking /proc/mdstat out will show the degraded array. If there was a spare disk available, reconstruction should have started.

Try with :

Now you’ve seen how it goes when a device fails. Let’s fix things up.

First, we will remove the failed disk from the array. Run the command

Now we have a /dev/md1 which has just lost a device. This could be a degraded RAID or perhaps a system in the middle of a reconstruction process. We wait until recovery ends before setting things back to normal.

We re-establish /dev/sdc2 back into the array.

As disk returns to the array, we’ll see it becoming an active member of /dev/md1 if necessary. If not, it will be marked as an spare disk.

Checking for errors and alerting

Steps for setting up e-mail alerting of errors with mdadm:

E-mail error alerting with mdadm can be accomplished in several ways:

1. Using a command line directly

2. Using the /etc/mdadm.conf file to specify an e-mail address

NOTE: e-mails are only sent when the following events occur:

Specifying an e-mail address using the mdadm command line

Using the command line simply involves including the e-mail address in the command. The following explains the mdadm command and how to set it up so that it will load every time the system is started.

The command could be put /etc/init.d/boot.local so that it was loaded every time the system was started.

Verification that mdadm is running can be verified by typing the following in a terminal window:

Creating a FreeBSD wireless access point

Access points are essentially wireless switches or hubs. Just like a switch or a hub, all clients communicate through the access point. FreeBSD allows us to easily create an access point with just very little configuration and just the right hardware

To set up a wireless access point using FreeBSD, you need to have a compatible wireless card. We are using a Prism 2-based chipset. For a complete list of cards that are supported, consult the man page for wi, or visit the Wireless Network Interface Section of the FreeBSD documentation site.

    • Configuring the kernel
  • Depending on how you wish to set up the access point will determine what options need to be added to the kernel config file. If the wireless network device is being installed on a server that is currently running as a Firewall/NAT, then we only need to compile the wireless device driver into the kernel:

    Choose the appropriate driver for your card from the list and include the wlan device, then recompile and install your kernel.

    If this the wireless network device is going to be installed on a system that does not serve as a Firewall/NAT, then we would want to include the BRIDGE option, along with the appropriate wireless device driver in the kernel config file.

    The bridging option will allow the wireless device to communicate with the wired ethernet interface. We must also add a couple of options to the /etc/sysctl.conf file in order to establish the bridge between the two interfaces:

    Be sure and replace fxp0 with whatever wired ethernet interface you are using with your FreeBSD installation. For information on bridging, consult the Bridging Section of the FreeBSD Handbook.

      Configuring the Wireless Interface

    The configuration of the wireless interface is fairly straightforward, we just need to add a few more options than if it were a wired ethernet interface. The following is an example of ifconfig options for a wireless interface:

    Of course this can all be setup in the /etc/rc.conf file so that these settings are retained every time the system boots. From this point, your access point should be up and broadcasting. There are just a couple more options to consider

      Post Configuration

    As stated earlier, if the wireless interface is installed in a server that is functioning as a Firewall/NAT, then the bridging option is unecessary. We just need to add a couple of rules to our firewall configuration files to allow traffic to be passed from the wireless interface.

    If you are using PF as your Firewall/NAT solution, simply add the following lines to your /etc/pf.conf file

    Replace wi0 with the appropriate interface name of your wireless card

    If you are using IPfilter as your Firewall/NAT solution, then simply add the following lines to your /etc/ipf.rules file

    Again, replace wi0 with the appropriate interface name of your wireless card.

      Administration

    Once the access point is configured and operational, we will want to see the clients that are associated with the access point. We can type the following command to get this information:

    Now you should have a complete functioning access point up and running. You are encouraged to read more about the wicontrol and wi commands for further information.

    Monitoring raw traffic on a Juniper Netscreen

    Occasionally I will run into situations where the only way to definitively diagnose network related problems is to perform raw traffic dumps on a main internal / external interface.

    The reasons for needing to perform this could be anything. I thought I’d share the quick and easy steps to perform in order to do a quick network traffic capture.

    Be warned though, that it is easy to overflow the console buffer and subsequently crash your firewall if you don’t narrow the scope of your capture enough.

    There exists a command on the juniper netscreen console that works the same way that tcpdump would, called “snoop”.

    To view the current snoop settings :

    To monitor all traffic from a particular ip address going to a particular port :

    To monitor all traffic on the network going to a particular ip address :

    The above commands only SET the filter. You have to turn the filter on and monitor the buffer to actually view the results. Note that you should ensure that the scope of your filters are quite narrow as there is the risk of overflowing the console buffer and crashing the firewall if you are monitoring a wide scope.

    To view the filters and turn on snoop :

    Dont forget to clear the filters , dbuf stream and turn off snoop when your done :

    That’s it!

    Dual Monitors in FreeBSD

    To those (few) of you out there that actually use FreeBSD as a workstation (myself included) , you may have had the opportunity to utilize dual monitors.

    As a Systems Administrator who is usually working on 3-4 things simultaneously , it is crucial to be able to function with enough screen space.

    One of the headaches I’ve encountered is trying to get my Dual monitors working with my Ati/Radeon video card in FreeBSD. I’ve written a little tutorial to help those who may need help or are thinking of implementing a second monitor.

    I found out my video driver and pci configuration by executing the following commands :

    I entered the following as my xorg.conf (ATI / RADEON video driver):

    Starting KDE / Xorg initially displays two identical monitors. You need to use the “xrandr” utility to utilize the dual monitor configuration. The following script, when run after starting KDE will do this for you :

    Optimizing the FreeBSD kernel

    Often we are asked by VPS clients utilizing the FreeBSD operating system, how can they trim down the kernel in order to utilize the full memory footprint potential.

    Without getting into too much detail, here are several things that we usually “omit” from the kernel options during make buildworld / buildkernel to provide for a 60-70% kernel footprint reduction in 7.1-PRERELEASE :

    makeoptions     DEBUG=-g                # Build kernel with gdb(1) debug symbols
    options         MSDOSFS                 # MSDOS Filesystem

    # Wireless NIC cards
    device          wlan            # 802.11 support
    device          wlan_wep        # 802.11 WEP support
    device          wlan_ccmp       # 802.11 CCMP support
    device          wlan_tkip       # 802.11 TKIP support
    device          wlan_amrr       # AMRR transmit rate control algorithm
    device          wlan_scan_ap    # 802.11 AP mode scanning
    device          wlan_scan_sta   # 802.11 STA mode scanning
    device          an              # Aironet 4500/4800 802.11 wireless NICs.
    device          ath             # Atheros pci/cardbus NIC’s
    device          ath_hal         # Atheros HAL (Hardware Access Layer)
    device          ath_rate_sample # SampleRate tx rate control for ath
    device          awi             # BayStack 660 and others
    device          ral             # Ralink Technology RT2500 wireless NICs.
    device          wi              # WaveLAN/Intersil/Symbol 802.11 wireless NICs.
    #device         wl              # Older non 802.11 Wavelan wireless NIC.

    device          ural            # Ralink Technology RT2500USB wireless NICs
    device          rum             # Ralink Technology RT2501USB wireless NICs

    You can remove more ,but that should reduce your kernel size significantly. You should be able to recompile the kernel as per the FreeBSD documentation