Chapter 13. Scripting for Administrators

The last chapter covered a major use of scripts for administrators: monitoring the systems you administer. Using MRTG, you can monitor CPU usage, available disk space, and network router throughput, among other things, but scripts come in handy in quite a few other areas. This chapter won't show you magical ways to run all of your systems. Instead, it describes how you can use scripts to improve your daily life and manage your systems with less work, including the following:

  • Deciding when and where to write scripts

  • Creating scripts in an organized fashion

  • Scripting complicated commands

  • Troubleshooting with scripts

  • Removing annoyances with scripts

  • Cleaning up yucky data formats

  • Automating your daily work with scripts

This chapter contains some very simple scripts and some complicated scripts. In all cases, though, the goal is to show techniques, not cleverness, and to focus on generating ideas for making your work easier.

Why Write Scripts?

From an administrator's point of view, scripts enable you to do the following:

  • Automate frequently run tasks

  • Remember complicated command-line options and file paths

  • Filter through data and respond with just the crucial items that require your attention

In these cases, scripts come in handy and, best of all, generally do not require a long time to write.

Scripting is fun. In about three minutes, you can create a useful script. The problem is that if you write all of your scripts in a totally ad hoc manner, you will end up with a confusing mess. If you approach your administration scripts with even a small amount of discipline, you will create a script library that will save you hours and hours of time.

Following a few simple guidelines will help you avoid ending up with a confusing mess:

  • Use a consistent style. Indent your scripts to make them easier to read. (Use any indenting style you prefer; you're not limited to the style shown by the examples in this book.)

  • Store your scripts in a common location. For example, store your scripts in /usr/local/bin or the bin subdirectory of your home directory, $HOME/bin. You might also want to separate your administrative scripts into a directory of their own.

  • Document your scripts. Comment liberally. You want to especially document why you wrote the script or what problem it helps to solve. In addition, specify how to call the script, the command-line arguments and options, and so on.

None of these guidelines should require much work or restrict your creativity.

Note that some very sick people have even gone so far as to write poetry in scripts, usually in another scripting language called Perl. Avoid these people. They are the first who will become zombies when the big meteor hits the Earth. Remember, you were warned.

You've probably noticed the focus on not spending a lot of time. In today's IT environments, administrators typically manage too many systems with too few resources. Spend your time going after the proverbial low-hanging fruit, the easy ones, first. Tackle the harder stuff later.

Scripting Complicated Commands

Computers can remember things very well. People can't always do the same. If you have commands you need to run, but you tend to forget the command-line arguments or options, you can solve the problem by storing the command in a script. For example, the following script has only one actual command.

The next example shows a few more lines of scripting commands but is similarly short.

While the scripting of complicated commands usually results in fairly small scripts, troubleshooting enables you to create more detailed scripts. Creating larger scripts does not necessarily show your scripting prowess. The goal, as always, is to solve problems.

Troubleshooting Your Systems

Just as writing larger scripts is not an end in itself, it is very important when troubleshooting that you don't end up reporting too much information. Therefore, when you create your troubleshooting scripts, focus on reporting only problems or essential information.

One of the most common problems on systems, especially those with minimal attention, is filling disks. To see how much space is used on a given disk or on all your system's disks, you can use the df command, as shown here:

$ df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda2             24193540   3980000  18984568  18% /
/dev/hda1               101086     10933     84934  12% /boot
none                    501696         0    501696   0% /dev/shm
/dev/hda5             48592392  25049972  21074036  55% /home2
/dev/sda1               507104    147936    359168  30% /media/TITAN

This example shows all the disks mounted on a particular system. The -k command-line option tells the df command to return the results in 1K blocks. You can then interpret the results to determine whether any of the disks requires further attention. Alternatively, you could write a script to perform the interpretation for you.

Another important task when troubleshooting a system is verifying that the necessary processes are running, such as a web server or a database, as shown in the following Try It Out.

You can place this type of check into a script, of course, leading to the next example.

Removing Minor Annoyances

Minor annoyances are anything that bugs you (anything small, that is, so Windows security issues don't count). You can use the capabilities of scripts to perform the necessary setup work for commands, so you don't have to.

Whenever possible, let the script do the work, as in the following example.

Cleaning Up Data

Many programs, systems, and devices log information or can describe themselves, sometimes in excruciating detail. It's the detail that is the problem. You can become overwhelmed by the massive amount of information to slog through just to get to the useful bits. As you'd suspect, scripts can help here as well.

Automating Daily Work

Anything you do day in and day out falls into this category. Use the techniques shown in the previous examples, as well as the previous chapters. Once you have created a script to automate some aspect of your work, you can schedule that script to run at a particular time.

The cron scheduler, introduced in Chapter 12, enables you to run commands on a periodic schedule. You can use cron by setting up a crontab file that specifies when to run the script. Other than the scheduling, however, this type of scripting is the same as any other kind.

Summary

Scripts are an administrator's best friend. They can remember things for you, speed up your work, and sort through yucky output, extracting just the nuggets of useful information necessary to make informed decisions.

This chapter focused on using scripts, and spending very little time writing them, for a big return in time saved for common administrative work. The scripts in this chapter are intended to provoke discussion and further thought about what you can do with scripts.

In the administrative arena, scripts can help you with the following:

  • Remembering complicated commands, command-line options, and command-line arguments for you

  • Troubleshooting, such as checking the amount of available disk space and whether certain types of processes are running

  • Removing minor annoyances

  • Digesting voluminous data and extracting useful information

  • Repetitive tasks you do every day

  • Anything else that bothers you or you think is ripe for scripting

The next chapter switches gears from boring administrative work to fun, graphical, splashy desktop automation. You can use scripts to help with desktop applications as well, especially on that most graphical of systems, Mac OS X.

Exercises

  1. Identify some issues at your organization and discuss how you can use scripts to help reduce the problems. (Stick to issues related to your computer systems, rather than annoying colleagues or dim-witted managers.)

  2. Look at the online documentation for the ps command. Discuss at least three useful things ps can report.

  3. Write a script to determine whether a given file system or mount point is mounted, and output the amount of free space on the file system if it is mounted. If the file system is not mounted, the script should output an error.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.170.134