Get Virtual Machines from VMware with PySphere
This is a simple example of how to get a list of Virtual Machines from a VMware host, using PySphere.
{% highlight python %} import pysphere
server = pysphere.VIServer() server.connect('vmware.zindilis.net', 'username', 'password')
vms = server.getregisteredvms() for vm in vms: virtualmachine = server.getvmbypath(vm) print virtualmachine.getproperty('name'), virtualmachine.getproperty('ip_address') {% endhighlight %}
Bash Special Variables
A couple of notes on special variables available in the Bash shell.
$0
: Name of the script
The special variable $0
contains the name of the script. It is useful when
returning usage instructions to the user that run the script. For example:
{% highlight bash %} echo "Usage: $0 --option-1 --option-2"; {% endhighlight %}
$#
: Number of arguments
The special variable $#
contains the number of arguments passed to the
script. If no arguments are passed, it is equal to 0. It is useful to examine
the value of this variable before taking further actions, if a script must
be run with a minimum number of arguments.
Here is an example of $#
used together with $0
:
{% highlight bash %} if [ $# -eq 0 ]; then echo "Insufficient number of arguments!"; echo "Usage: $0 /path/to/file"; fi {% endhighlight %}
$?
: Most recent exit code
The special variable $?
contains the exit code of the most recently
executed command. It is useful to examine this to determine whether or
not the previous command completed succesfully or returned an error.
Typically, commands that complete succesfully return an exit code equal to 0, with other values indicating different errors, but check each command's documentation to verify this.
In the following example, the existense of the word "test" in a file is checked:
{% highlight bash %} grep test /path/to/file > /dev/null; if [ $? -eq 0 ]; then echo "/path/to/file contains the word 'test'"; else echo "/path/to/file does not contain the word 'test'"; fi {% endhighlight %}
screen
Links
- The article Time-Saving Tricks at the Command Line has a section for
working with
screen
with many helfpul tips.
Enable EPEL Repository on CentOS 6
The most updated information for enabling EPEL should always be at the Fedora Project's Wiki.
In that page's section "How can I use these extra packages?", there are links to the latest versions for CentOS 6 (and CentOS 5).
In that page, download the .noarch
file, for example:
wget http://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Then, install with yum
:
yum install epel-release-*.noarch
ntpdate
Troubleshooting no server suitable for synchronization
If you have a cron jon periodically running ntpdate
to update the
time on a system, you might at some point hit the no server suitable
for synchronization
error. You can gain some insight into the problem
with running ntpdate
with the -d
option, which will show debugging
output from the actions of ntpdate
, without actually changing the
time on the system.
One example cause of this issue is that the value of strata exceeds
a maximum value, in which case the NTP server is rejected by the system
as being too far from its own source. You should then focus on either
fixing the NTP server itself, or changing your ntpdate
target to
another system.
Enable logging in MySQL
These are some haphazardly written notes on logging in MySQL. For consistency,
create a new subdirectory in /var/log/
and assign ownership to the user under
which your MySQL server is running - by default that would be the mysql
user:
mkdir /var/log/mysqld/
chown mysql:mysql /var/log/mysqld
I like to name the directory mysqld
instead of mysql
, to clarify
that the logs in that directory are those of the MySQL Server and
not those of the MySQL Client.
Slow Query Log
The slow query log contains queries the execution of which took more
than the value of the long_query_time
variable, which by default is
10 seconds.
To enable the slow query log on the next start of mysqld
, add the
following line in your MySQL server's configuration file (/etc/my.cnf
by default), in the [mysqld]
section:
slow-query-log
slow_query_log_file=/var/log/mysqld/slow-queries.log
Create the file and assign ownership to the mysql
user:
touch /var/log/mysqld/slow-queries.log
chown mysql:mysql /var/log/mysqld/slow-queries.log
Finally, either restart the server to enable the logging, or enable it on the already running server with:
mysql> SET GLOBAL slow_query_log_file='/var/log/mysqld/slow-queries.log';
mysql> SET GLOBAL slow_query_log=1;
In any of those cases, the values of the global variables
slow_query_log
and slow_query_log_file
, should change from the
default:
mysql> show variables like 'slow_query_log%';
+---------------------+----------------------------------+
| Variable_name | Value |
+---------------------+----------------------------------+
| slow_query_log | OFF |
| slow_query_log_file | /var/run/mysqld/mysqld-slow.log |
+---------------------+----------------------------------+
To:
mysql> show variables like 'slow_query_log%';
+---------------------+----------------------------------+
| Variable_name | Value |
+---------------------+----------------------------------+
| slow_query_log | ON |
| slow_query_log_file | /var/log/mysqld/slow-queries.log |
+---------------------+----------------------------------+