Chef notifies and subscribes explained with examples

chef-notifies-and-subscribes-explained-with-examples

 

Chef notifies and subscribes explained with examples

A notification is a property on a resource that listens to other resources in the resource collection and then takes actions based on the notification type (notifies or subscribes).

 

Timers

A timer specifies the point during the chef-client run at which a notification is run. The following timers are available:
:before
Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.
:delayed
Default. Specifies that a notification should be queued up, and then executed at the very end of the chef-client run.
:immediate, :immediately
Specifies that a notification should be run immediately, per resource notified.

 

Notifies

A resource may notify another resource to take action when its state changes. Specify a ‘resource[name]’, the :action that resource should take, and then the :timer for that action. A resource may notify more than one resource; use a notifies statement for each resource to be notified.
The syntax for notifies is:
notifies :action, ‘resource[name]’, :timer

 

Example
The following examples show how to use the notifies notification in a recipe.

 

Delay notifications
template ‘/etc/nagios3/configures-nagios.conf’ do
  # other parameters
  notifies :run, ‘execute[test-nagios-config]’, :delayed
end

 

Notify immediately
By default, notifications are :delayed, that is they are queued up as they are triggered, and then executed at the very end of a chef-client run. To run an action immediately, use :immediately:
template ‘/etc/nagios3/configures-nagios.conf’ do
  # other parameters
  notifies :run, ‘execute[test-nagios-config]’, :immediately
end
and then the chef-client would immediately run the following:
execute ‘test-nagios-config’ do
  command ‘nagios3 –verify-config’
  action :nothing
end

 

Subscribes

A resource may listen to another resource, and then take action if the state of the resource being listened to changes. Specify a ‘resource[name]’, the :action to be taken, and then the :timer for that action.
Note that subscribes does not apply the specified action to the resource that it listens to – for example:
file ‘/etc/nginx/ssl/example.crt’ do
   mode ‘0600’
   owner ‘root’
end
service ‘nginx’ do
   subscribes :reload, ‘file[/etc/nginx/ssl/example.crt]’, :immediately
end
In this case the subscribes property reloads the nginx service whenever its certificate file, located under /etc/nginx/ssl/example.crt, is updated. subscribes does not make any changes to the certificate file itself, it merely listens for a change to the file, and executes the :reload action for its resource (in this example nginx) when a change is detected.
The syntax for subscribes is:
subscribes :action, ‘resource[name]’, :timer

 

Examples
The following examples show how to use the subscribes notification in a recipe.
Prevent restart and reconfigure if configuration is broken
Use the :nothing action (common to all resources) to prevent the test from starting automatically, and then use the subscribes notification to run a configuration test when a change to the template is detected:
execute ‘test-nagios-config’ do
  command ‘nagios3 –verify-config’
  action :nothing
  subscribes :run, ‘template[/etc/nagios3/configures-nagios.conf]’, :immediately
end

 

Reference
Example
Tagged : / / / / / / / /

20 pmap Commands Examples in Linux / UNIX | pmap Commands Tutorial

You can find the memory used by a program (process) by looking into /proc directory or using standard command such as ps or top.

However, you must calculate all memory usage by hand i.e. add Shared Memory + mapped file + total virtual memory size of the process + Resident Set Size + non-swapped physical memory used by process. So how do you find the total memory used by a process or program under Linux? Use a tool called pmap. It reports the memory map of a process or processes.
To display process mappings, type
$ pmap pid
$ pmap 3724
If you want to see the exented format
The -x option can be used to provide information about the memory allocation and mapping types per mapping. The amount of resident, non-shared anonymous, and locked memory is shown for each mapping:
$ pmap -x 3526
If you want to see the device format
$ pmap -d 3526
If you do not display some header/footer lines.
$ pmap -q 3526
GENERAL OPTIONS
-x extended Show the extended format.
-d device Show the device format.
-q quiet Do not display some header/footer lines.
-V show version Displays version of program.
Reference
Tagged : / / / / / / / / /

20 mpstat Commands Examples in Linux / UNIX | mpstat Commands Tutorial

If you are using SMP (Multiple CPU) system, use mpstat command to display the utilization of each CPU individually.
mpstat Report processors related statistics which includes Collecting and displays performance statistics for all logical processors in the system.. This display CPU statistics of individual CPU (or) Core.
How to install mpstat?
1. Ensure you are logged in as ‘root’
‘mpstat’ and ‘vmstat’ are apart of the ‘sysstat’ package and should be installed by default
2. Verify installation :
# rpm -qa | grep -i sysstat
3. Installation, if needed :
# rpm -ivh <name>
4. If you have connectivity to RHN or a Satellite Server:
# yum install sysstat
5. If you are using Ubantu:
# apt-get install sysstat

Usage:

1. Using mpstat command without any option, will display the Global Average Activities by All CPUs.
$ mpstat
2. Using mpstat with option ‘-P’ (Indicate Processor Number) and ‘ALL’, will display statistics about all CPUs one by one starting from 0. 0 will the first one.
$ mpstat -P ALL
3. To display the statistics for N number of iterations after n seconds interval with average of each cpu use the following command.
$ mpstat -P ALL 2 5
4. The option ‘I’ will print total number of interrupt statistics about per processor.
$ mpstat -I
5. Get all the above information in one command i.e. equivalent to “-u -I ALL -p ALL”.
$ mpstat -A
Reference
Tagged : / / / / / / / / /

20 iostat Commands Examples in Linux / UNIX | iostat Commands Tutorial

iostat command is a command that used for monitoring system input/output device loading by observing the time the devices are active in relation to their average transfer rates. The iostat create reports that can be used to change system configuration to better balance the input/output between physical disks.
You can use iostat command which report Central Processing Unit (CPU) statistics and input/output statistics for devices and partitions. It can be use to find out your system’s average CPU utilization since the last reboot.
Reports Central Processing Unit (CPU) statistics, asynchronous input/output (AIO) and input/output statistics for the entire system, adapters, TTY devices, disks CD-ROMs, tapes and file systems.
Note:
iostat reports CPU, disk I/O, and NFS statistics.
vmstat reports virtual memory statistics.
mpstat reports processors statictics.
How to install iostat?
1. Ensure you are logged in as ‘root’
‘iostat’ and ‘vmstat’ are apart of the ‘sysstat’ package and should be installed by default
2. Verify installation :
# rpm -qa | grep -i sysstat
3. Installation, if needed :
# rpm -ivh <name>
4. If you have connectivity to RHN or a Satellite Server:
# yum install sysstat
5. If you are using Ubantu:
# apt-get install sysstat
1. iostat – Basic example, Iostat without any argument displays information about the CPU usage, and I/O statistics about all the partitions on the system as shown below.
$ iostat
2. iostat – Display only cpu statistics. iostat option -c, displays only the CPU usage statistics as shown below.
$ iostat -c
3. iostat – Display only disk I/O statistics. iostat option -d, displays only the disk I/O statistics as shown below.
$ iostat -d
4. iostat – Display only network statistics. iostat option -n, displays only the device and NFS statistics as shown below.
$ iostat -n
5. iostat – Display I/O data in MB/second. By default iostat, displays the device I/O statistics in Blocks. To change it to MB, use -m as shown below.

$ iostat -m

6. iostat – Display I/O statistics only for a device. By default iostat displays I/O data for all the disks available in the system. To view statistics for a specific device (For example, /dev/sda), use the option -p as shown below.
$ iostat -p sda
7. iostat – Display timestamp information, By default iostat displays only the current date. To display the current time, use the option -t as shown below.
$ $ iostat -t
8. iostat – Display Extended status, Use option -x, which will displays extended disk I/O statistics information as shown below.
$ iostat -x
9. To display extended information for a specific partition (For example, /dev/sda1), do the following.
$ iostat -x sda1
10. iostat – Execute Every x seconds (for y number of times). To execute iostat every 2 seconds (until you press Ctl-C), do the following.
$ iostat 2
11.To execute every 2 seconds for a total of 3 times, do the following.
$ iostat 2 3
12. iostat – Display LVM statistic (and version)
$ iostat -N
13. To display the version of iostat, use -V. This will really display the version information of sysstat, as iostat is part of sysstat package.
$ iostat -V
Reference
iostat use these files to create reports…
/proc/stat which contains system statistics
/proc/partitions which contains disk statistics (for pre 2.5 kernel that have been patched)
/proc/diskstats contains disks statistics (for post 2.5 kernel)
/sys which contains statistics for block devices (post 2.5 kernel)
Tagged : / / / / / / / / /

20 vmstat Commands Examples in Linux / UNIX | vmstat Commands Tutorials

vmstat-commands
vmstat – Report virtual memory statistics. vmstat is a tool that collects and reports data about your system’s memory, swap, and processor resource utilization in real time. It can be used to determine the root cause of performance and issues related to memory use.
vmstat reports information about processes, memory, paging, block IO, traps, and cpu activity. The first report produced gives averages since the last reboot. Additional reports give information on a sampling period of length delay. The process and memory reports are instantaneous in either case.
Note:
iostat reports CPU, disk I/O, and NFS statistics.
vmstat reports virtual memory statistics.
mpstat reports processors statictics.
How to install vmstat?
1. Ensure you are logged in as ‘root’
‘iostat’ and ‘vmstat’ are apart of the ‘sysstat’ package and should be installed by default
2. Verify installation :
# rpm -qa | grep -i sysstat
3. Installation, if needed :
# rpm -ivh <name>
4. If you have connectivity to RHN or a Satellite Server:
# yum install sysstat
5. If you are using Ubantu:
# apt-get install sysstat
Example
1. The significant of the columns are explained in man page of vmstat in details. Most important fields are free under memory and si, so under swap column.
[root@tecmint ~]# vmstat -a
procs ———–memory———- —swap– —–io—- –system– —–cpu—–
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 810420  97380  70628    0    0   115     4   89   79  1  6 90  3  0
Free – Amount of free/idle memory spaces.
si – Swaped in every second from disk in Kilo Bytes.
so – Swaped out every second to disk in Kilo Bytes.
2. Execute vmstat ‘X’ seconds and (‘N’number of times). With this command, vmstat execute every two seconds and stop automatically after executing six intervals.
# vmstat 2 6
3. Vmstat with timestamps. vmstat command with -t parameter shows timestamps with every line printed as shown below.
# vmstat -t 1 5
4. Statistics of Various Counter, vmstat command and -s switch displays summary of various event counters and memory statistics.
$ vmstat -s
5. Disks Statistics, vmstat with -d option display all disks statistics.
$ vmstat -d
6. Display Statistics in Megabytes, The vmstat displays in Megabytes with parameters -S and M(Uppercase & megabytes). By default vmstat displays statistics in kilobytes.
# vmstat -S M 1 5
7. vmstat – Display number of forks since last boot, This displays all the fork system calls made by the system since the last boot. This displays all fork, vfork, and clone system call counts.
# vmstat -f
8. vmstat – Display slab info, Use option -m, to display the slab info as shown below.
# vmstat -m
Reference
The output of vmstat is displayed in a number of columns. The following sections provide brief overviews of the data reported in each column.
Procs
The procs data reports the number of processing jobs waiting to run and allows you to determine if there are processes “blocking” your system from running smoothly.
The r column displays the total number of processes waiting for access to the processor. The b column displays the total number of processes in a “sleep” state.
These values are often 0.
r: The number of processes waiting for run time.
b: The number of processes in uninterruptible sleep.
Memory
The information displayed in the memory section provides the same data about memory usage as the command free -m.
The swapd or “swapped” column reports how much memory has been swapped out to a swap file or disk. The free column reports the amount of unallocated memory. The buff or “buffers” column reports the amount of allocated memory in use. The cache column reports the amount of allocated memory that could be swapped to disk or unallocated if the resources are needed for another task.
swpd: the amount of virtual memory used.
free: the amount of idle memory.
buff: the amount of memory used as buffers.
cache: the amount of memory used as cache.
inact: the amount of inactive memory. (-a option)
active: the amount of active memory. (-a option)
Swap
The swap section reports the rate that memory is sent to or retrieved from the swap system. By reporting “swapping” separately from total disk activity, vmstat allows you to determine how much disk activity is related to the swap system.
The si column reports the amount of memory that is moved from swap to “real” memory per second. The so column reports the amount of memory that is moved to swap from “real” memory per second.
si: Amount of memory swapped in from disk (/s).
so: Amount of memory swapped to disk (/s).
I/O
The io section reports the amount of input and output activity per second in terms of blocks read and blocks written.
The bi column reports the number of blocks received, or “blocks in”, from a disk per second. The bo column reports the number of blocks sent, or “blocks out”, to a disk per second.
bi: Blocks received from a block device (blocks/s).
bo: Blocks sent to a block device (blocks/s).
System
The system section reports data that reflects the number of system operations per second.
The in column reports the number of system interrupts per second, including interrupts from system clock. The cs column reports the number of context switches that the system makes in order to process all tasks.
in: The number of interrupts per second, including the clock.
cs: The number of context switches per second.
CPU
The cpu section reports on the use of the system’s CPU resources. The columns in this section always add to 100 and reflect “percentage of available time”.
The us column reports the amount of time that the processor spends on userland tasks, or all non-kernel processes. The sy column reports the amount of time that the processor spends on kernel related tasks. The id column reports the amount of time that the processor spends idle. The wa column reports the amount of time that the processor spends waiting for IO operations to complete before being able to continue processing tasks.
These are percentages of total CPU time.
us: Time spent running non-kernel code. (user time, including nice time)
sy: Time spent running kernel code. (system time)
id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.
Field Description For Disk Mode
Reads
total: Total reads completed successfully
merged: grouped reads (resulting in one I/O)
sectors: Sectors read successfully
ms: milliseconds spent reading
Writes
total: Total writes completed successfully
merged: grouped writes (resulting in one I/O)
sectors: Sectors written successfully
ms: milliseconds spent writing
IO
cur: I/O in progress
s: seconds spent for I/O
Field Description For Disk Partition Mode
reads: Total number of reads issued to this partition
read sectors: Total read sectors for partition
writes : Total number of writes issued to this partition
requested writes: Total number of write requests made for partition
Field Description For Slab Mode
cache: Cache name
num: Number of currently active objects
total: Total number of available objects
size: Size of each object
pages: Number of pages with at least one active object
totpages: Total number of allocated pages
pslab: Number of pages per slab
Files
/proc/meminfo
/proc/stat
/proc/*/stat
Tagged : / / / / / / / / /

20 Xargs Commands Examples in Linux / UNIX | Xargs Commands Tutorials

xargs-commands

Under Development

The xargs command is extremely useful when we combine it with other commands.This tutorials explains the usage of xargs command using few simple examples.
1. Xargs Basic Example
The xargs command (by default) expects the input from stdin, and executes /bin/echo command over the input. When you execute xargs without any argument, or when you execute it without combining with any other commands. you get Welcome message
> xargs
Hi,
Welcome to TGS.Hi, Welcome to TGS.
After you type something, press ctrl+d, which will echo the string back to you on stdout as shown below.
2. Specify Delimiter Using -d option
Delimiters can be applied so that each character in the input is taken literally using -d option in xargs.In the following example, when you use the -d\n, it will preserve newline delimiter in the output, and display the output exactly as it was typed.
> xargs -d\n
Hi,
Welcome to TGS.
3. Limit Output Per Line Using -n Option
By default as explained earlier, xargs displays whatever comes to its stdin as shown below.
> echo a b c d e f| xargs
a b c d e f
> echo a b c d e f| xargs -n 3
a b c
d e f
In the following example, we used -n 3, which will display only 3 items per line in the xargs output.
4. Delete Files that has White-space in the Filename
> find . -name “*.c” -print0 | xargs -0 rm -rf
Example
# fild /usr/preserve –mtime +30 –exec rm –f {} \;
is equivalent to….
# find /usr/preserve –mtime +30 –print | xargs rm –f
# find /usr/preserve –size +1024 –print | xargs –n20 rm –f
Tagged : / / / / / / / / /

Understand Ant command line arguments with Examples

ant-command-line-arguments

Several tasks take arguments that will be passed to another process on the command line. To make it easier to specify arguments that contain space characters, nested arg elements can be used.

value – a single command-line argument; can contain space characters.

file – The name of a file as a single command-line argument; will be replaced with the absolute filename of the file.

path – A string that will be treated as a path-like string as a single command-line argument; you can use ; or : as path separators and Ant will convert it to the platform’s local conventions.

pathref – Reference to a path defined elsewhere. Ant will convert it to the platform’s local conventions.

line – a space-delimited list of command-line arguments.

It is highly recommended to avoid the line version when possible. Ant will try to split the command line in a way similar to what a (Unix) shell would do, but may create something that is very different from what you expect under some circumstances.

Examples

<arg value=”-l -a”/>

is a single command-line argument containing a space character.

<arg line=”-l -a”/>

represents two separate command-line arguments.

<arg path=”/dir;/dir2:\dir3″/>

is a single command-line argument with the value \dir;\dir2;\dir3 on DOS-based systems and /dir:/dir2:/dir3 on Unix-like systems.

Command-line Options Summary
ant [options] [target [target2 [target3] …]]

Options:
-help, -h Displays help information describing the Ant command and its options
-projecthelp, -p Print project help information
-version Print the version information and exit
-diagnostics Print information that might be helpful to diagnose or report problems.
-quiet, -q Suppresses most messages not originated by an echo task in the buildfile
-verbose, -v Displays detailed messages for every operation during a build.
-debug, -d Print debugging information
-emacs, -e Produce logging information without adornments
-lib <path> Specifies a path to search for jars and classes
-logfile <file> Use given file for log
-l <file> Use given file for log
-logger <classname> Specifies a class to handle Ant logging.
-listener <classname> Add an instance of class as a project listener
-noinput Do not allow interactive input
-buildfile <file> Use given buildfile
-file <file> Use given buildfile
-f <file> Use given buildfile
-D<property>=<value> Defines a property name-value pair on the command line.
-keep-going, -k execute all targets that do not depend on failed target(s)
-propertyfile <name> load all properties from file with -D properties taking precedence
-inputhandler <class> the class which will handle input requests
-find <file> Search for buildfile towards the root of the filesystem and use it
Tagged : / / / / / / / / / / / / /

Ant command line arguments – Examples – Summary

ant-command-line-arguments

Ant command line arguments
Several tasks take arguments that will be passed to another process on the command line. To make it easier to specify arguments that contain space characters, nested arg elements can be used.

value – a single command-line argument; can contain space characters.
fileThe name of a file as a single command-line argument; will be replaced with the absolute filename of the file.
pathA string that will be treated as a path-like string as a single command-line argument; you can use ; or : as path separators and Ant will convert it to the platform’s local conventions.
pathref Reference to a path defined elsewhere. Ant will convert it to the platform’s local conventions.
line a space-delimited list of command-line arguments.

It is highly recommended to avoid the line version when possible. Ant will try to split the command line in a way similar to what a (Unix) shell would do, but may create something that is very different from what you expect under some circumstances.

Examples
<arg value=”-l -a”/>
is a single command-line argument containing a space character.
<arg line=”-l -a”/>
represents two separate command-line arguments.
<arg path=”/dir;/dir2:\dir3″/>
is a single command-line argument with the value \dir;\dir2;\dir3 on DOS-based systems and /dir:/dir2:/dir3 on Unix-like systems.
Command-line Options Summary
ant [options] [target [target2 [target3] …]]

Options:

-help, -h Displays help information describing the Ant command and its options
-projecthelp, -p Print project help information
-version Print the version information and exit
-diagnostics Print information that might be helpful to diagnose or report problems.
-quiet, -q Suppresses most messages not originated by an echo task in the buildfile
-verbose, -v Displays detailed messages for every operation during a build.
-debug, -d Print debugging information
-emacs, -e Produce logging information without adornments
-lib <path> Specifies a path to search for jars and classes
-logfile <file> Use given file for log
-l <file> Use given file for log
-logger <classname> Specifies a class to handle Ant logging.
-listener <classname> Add an instance of class as a project listener
-noinput Do not allow interactive input
-buildfile <file> Use given buildfile
-file <file> Use given buildfile
-f <file> Use given buildfile
-D<property>=<value> Defines a property name-value pair on the command line.
-keep-going, -k execute all targets that do not depend on failed target(s)
-propertyfile <name> load all properties from file with -D properties taking precedence
-inputhandler <class> the class which will handle input requests
-find <file> Search for buildfile towards the root of the filesystem and use it
Tagged : / / / / / / / / / / / /