Linux find command (exec vs xargs)

No comments:
As a matter of fact, i detest having to learn more than one method to achieve a job when it comes to shell scripting. But most of the time, sysadmins should find their needs to be met in the best way.

Find has the -exec option to perform actions on the files that are found. It is a common way of deleting unnecessary files without xargs.

$ find . -name "*.tmp" -type f -exec rm -f {} \;

In the above example "{}" is safe to substitute for every file with a space in its name. But "rm" command is executed once for every single file that is found. If we think about tons of files to be removed then a lot of fork processes are likely to take place.

How about using xargs:

$ find . -name "*.tmp" -type f -print0 | xargs -0 -r rm -f

With xargs, "rm" will be executed once for all files, decreasing overhead of the fork. It would be safe to use "-print0" option for files with space. Xargs "-r" option is for not running if stdin is empty. Of course there is a limit for the argument list xargs can have at a time. Otherwise xargs will split the input and try to execute the command repeatedly. With "-s" flag this limit can be overriden.

Ansible Playbook for cleaning all print jobs

No comments:
- hosts: print_servers
    - name: Clears all print jobs from the queues of the specified printers.
      shell: for i in $(/usr/bin/lpstat -o {{ item }} | awk '{ print $1 }'); do /usr/bin/cancel $i; done
        - printer1
        - printer2

Randomly Generating User Passwords Using Ansible

No comments:
First, i would like to note that i have recently started using Ansible for configuration management. One of the things i need in my server environment is to implement a user password changing policy. Since the targets are numerous, i have to use randomly generated passwords for each host. Because passwords are sensitive, they should be generated in the encrypted form. Ansible documentation recommends using python passlib library and SHA512 hashing here.

Ansible requires python-simplejson when Python version is 2.4

No comments:
Ansible requires python-simplejson package when python version is 2.4. | FAILED >> {
    "failed": true,
    "msg": "Error: ansible requires a json module, none found!OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 19: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug1: mux_client_request_session: master session id: 2\r\nShared connection to closed.\r\n",
    "parsed": false

On Redhat 5, following package should be installed:

# rpm -ivh python-simplejson-2.0.9-8.el5.x86_64.rpm

Then from ansible:

# ansible server11 -m ping | success >> {
    "changed": false,
    "ping": "pong"

Adding Bulk New Contacts to Microsoft Active Directory

No comments:
Sometimes it is a pain for SysAdmins to add objects to Windows AD. In this example i have provided a visual basic script which reads information about some mail enabled contacts from a tab seperated text file, then create them in the Active Directory.

Every line of the text file includes:
Contact Name
First Name
Phone Number

Collectd Configuration for snmp and write-graphite plugins

No comments:
This configuration is useful for polling performance metrics from servers using collectd snmp plugin and writing statistics data to graphite whisper database and visualize them.

collectd.conf file as follows:

LoadPlugin snmp
LoadPlugin write_graphite

Graphite carbon-cache IOError with too many open files

No comments:
When running carbon-cache daemon, clients are seeing errors such as connection refused by the daemon, a common reason for this is setting small number of file descriptors.

/var/log/carbon/console.log file, there may be

exceptions.IOError: [Errno 24] Too many open files: '/var/lib/graphite/whisper/systems/<host_name>/<metric_name>.wsp'

The number of files carbon-cache daemon can open should be increased. Many Linux systems set file descriptors to a maximum of 1024 as default. A value of 16384 may be good enough depending on how many clients are simultaneously connecting to the carbon-cache daemon.

In Linux, sysctl and ulimit programs can be used to set system-wide resource use.