Chris Tate-Davies

An archive of helpful tit bits of information for development, and probably some stuff that is incomplete, wrong or boring…

Automatic Windows Process Dump Script

Posted on | June 14, 2011 | No Comments

I have a laborious task at the moment, of exporting our server process list to see how much memory certain programs are using. I was getting a little bored of remote accessing the server through VNC so decided to use a BAT file to do the work for me. However the problem was, I wanted to save each dump as the date it was run and this wasn’t easy in a BAT file.

C:> TASKLIST.EXE /V /FO CSV > 2011-06-14.txt

That is the function I have been using. So this is what I want the script to do.

To get the actual current date, I used this call:

ver | date > date.txt

This creates a file called date.txt and inserts the current date (with another line which I can ignore)

So, now I want to read this file into memory so I can get the date from it.

set /p var= <date.txt

This reads in the first line of the file and stores it in the var variable

The line is:

The current date is: 14/06/2011

So lets get rid of the preliminary string as we don’t want it

set var=%var:~21,10%

This sets the variable “var” to be a substring of itself, starting at character 21 and fo 10 characters. Get it?

All we need to do now is chop and change the date so I get it in the format I want, from dd/mm/yyyy to yyyy-mm-dd (I am a database sucker, so always use these format dates)

A little more confusing looking, but does the same as the previous line, picking bits of the string and putting them into the var variable.

set var=%var:~6,4%-%var:~3,2%-%var:~0,2%.txt

If you read it, you will see it takes the 4 characters starting from position 6 (this is the year), adds a “-” symbol, then 2 characters starting from the 3rd, another – and then the first 2 characters

Put it all into a batch file, and it looks like this:

@echo off
REM Chris Tate-Davies
REM Export the current process list and save as todays date.txt
REM To be run from the schheduled tasks every day

REM delete any existence of previous date
del date.txt

REM Get the date and put it in a file
ver | date > date.txt

REM open the new file and put the date in variable "var"
set /p var= <date.txt

REM chop out the string so we just have the date
set var=%var:~21,10%

REM re-allign the date parts to yyyy-mm-dd
set var=%var:~6,4%-%var:~3,2%-%var:~0,2%.txt

REM run the tasklist and pass the %var% to output to
tasklist.exe /V /FO CSV > %var%

Now all I need to do is add it to the scheduled tasks and then I can pick up the files once a week instead or stopping what I am doing every day to do it.

 

 

CSV file of Windows Process list

Posted on | June 10, 2011 | No Comments

If you need a CSV file of the currently running  processes in Windows you can use the task manager command line tool.

C:> TASKLIST.EXE

This will dump the task list to the screen. But there are several options you can use:

C:> TASKLIST.EXE /V /FO CSV > filename.csv

The above will produce a nicely formatted CSV file with information about the process, such as CPU Time and memory used.

The full list of other options can be found at the Microsoft Technet library, well, until they move the location of it… http://technet.microsoft.com/en-us/library/bb491010.aspx

 

PHP App Testing

Posted on | June 9, 2011 | No Comments

Think: “What am I testing?”

1) Set-up

Instantiate your class

Look for dependencies, are there any that need mocking?

2) Exercise

We test our real objects that need testing,

or mock objects that don’t but are required by SUT (System Under Test)

3) Verify

Real object results

Expectations of mocked objects

4) Tear down

Destroy objects if needed?

 

The four-phased testing technique explained here: http://xunitpatterns.com/Four%20Phase%20Test.html

 

Resetting forgotten MySQL root password

Posted on | May 27, 2011 | No Comments

If you have forgotten your root password for MySQL, then don’t panic. Although, if you don’t have root shell access, then do panic!

Anyway, to reset a password, you need to stop MySQL and restart it with a special setting that allows you to login without passwords. Obviously this is a dangerous switch and you have to remember to stop and start MySQL after you’ve finished!

Linux:

Login with superuser access (sudo su)

> /etc/init.d/mysql stop

This will stop the MySQL service on the server, and all connections will be cut off.

Then we need to start MySQL with the secret switch:

> mysqld_safe --skip-grant-tables &

I don’t get any messages after this, but you may get one saying the mysqld_safe has started.

Then connect as root:

> mysql -u root

You will be connected. And you can just assign the root user a new password:

mysql> USE mysql;
mysql> UPDATE user SET password=PASSWORD(NEWPASSWORD) WHERE user = "root";
mysql> FLUSH PRIVILEGES;
mysql> exit;

That’s it. So now, just stop and start mysql again (as superuser) and were done:

> /etc/init.d/mysql restart

 

Changing your MySQL root password

Posted on | May 27, 2011 | No Comments

When you installed MySQL, you would have been asked for a root password. If you were testing or something similar, you may have used an empty password. This is all well and good until you need to put your server into production. So, how do you create one?

> mysqladmin -u root password NEWPASSWORD

This will assign the root user, the password in NEWPASSWORD

But, what if you want to change the root’s password?

> mysqladmin -u root -pOLDPASSWORD password NEWPASSWORD

i.e.

> mysqladmin -u root -pabcdefg password gfedcba

The above example will change the root’s password (if it is right) from abcdefg to gfedcba (I know this isn’t a very strong password, but its only an example)

You can of course, log into MySQL to change the password for any user by following these commands:

> mysql -u root -pADMINPASSWORD
mysql> USE mysql;
mysql> UPDATE user SET password=PASSWORD('NEWPASSWORD') WHERE user = "root";
mysql> FLUSH PRIVILEGES;

Done.

If you forget your root password, then see my post on forgotten MySQL root password

 

How to pass data from Controller to Zend Form

Posted on | May 20, 2011 | No Comments

To pass data from your controller, to a Zend_Form, you can utilise the config parameters:

$this->view->form = new Contact_Edit_Form(array('contactName' => 'Chris'));

This will pass a very simple array to the form, and from within the form,

$data = $this->getAttrib('contactName');

$data will then contain Chris


Zend Console – get values passed

Posted on | May 11, 2011 | No Comments

When using a CLI script with options, I like to use the Zend_Console.

If my options are as follows:

$console = new Zend_Console_Getopt(
    array(
        'i-s'  => 'test option 1',
        'e'    => 'test option 2')
    );

For instance,

> php myScript.php -i OPTION

To get the “OPTION” string for the -i parameter

$arg = $console->getOption('i');

$arg will now contain the passed option, but if none was passed, it will be NULL

When outlining the available options that can be used with your script, you use the following syntax:

long|short  => description;
test|t => 'Test the script';

You can use,

> php myScript.php -t

or

> php myScript.php --test

to run the -test parameter. You can also pass values, such as an integer or a string:

'test|t=i' => 'Option with required integer parameter';
'test|t-s' => 'Option with optional string parameter'

For more information, refer to the Zend Documentation : http://framework.zend.com/manual/en/zend.console.getopt.introduction.html

JQuery – bind event to non existant element

Posted on | May 10, 2011 | No Comments

We all have to bind events to elements:

$('#link).click(function(){
    do some stuff
 });

But, what if that element doesn’t exist yet? Well, you can use the “live” handler:

$('#link').live('click', function(){
    do some stuff
 });

This will carry the event handler on and will match any future element that matches the selector.

More information can be found here: http://api.jquery.com/live/

Facebook – Download *all* of your data

Posted on | May 4, 2011 | No Comments

You can download all the data you’ve ever uploaded to Facebook if you like.

Once you are logged in, click this link: https://www.facebook.com/editaccount.php?ref=mb&drop and press the “Download your information” link.

It will take a while as it packages up all images and videos too.

Facebook will email you when it’s ready and hey hoe, you can download it to see how much of a facebook whore you are!

 

MySQL Query Profiling

Posted on | April 27, 2011 | No Comments

This inbuilt part of MySQL allows you to look closely at queries run – per session. And you can use this information to find bottlenecks. To enable profiling, run this command in the MySQL client:

SET profiling = 1;

This will turn on the profiling. Then for each query you run, MySQL will log all the queries and extra information about them.

Run yourself some queries, and then run this command:

SHOW PROFILES;

It will show you a standard MySQL recordset of the queries you have run, each with an individual ID, in the order that you ran them.

+----------+-------------+--------------------------------------------------------+
| Query_ID | Duration    | Query                                                  |
+----------+-------------+--------------------------------------------------------+
|        1 |  0.00009260 | SELECT fieldname FROM table WHERE field = 'value'      |
+----------+-------------+--------------------------------------------------------+
There will be as many rows as queries that you ran. And that’s not it, you can drill down even more, to see what the duration is made up with… To drill down on a query, make a note of the Query_ID and use the following statement:
SHOW PROFILE FOR QUERY 1;
The resultset will be a list of operations that MySQL ran in order to complete the query. For example:
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000033 |
| checking query cache for query | 0.000073 |
| Opening tables                 | 0.000013 |
| System lock                    | 0.000007 |
| Table lock                     | 0.000035 |
| init                           | 0.000032 |
| optimizing                     | 0.000014 |
| statistics                     | 0.000016 |
| preparing                      | 0.000014 |
| executing                      | 0.000009 |
| Sending data                   | 0.331296 |
| end                            | 0.000016 |
| end                            | 0.000003 |
| query end                      | 0.000005 |
| storing result in query cache  | 0.000105 |
| freeing items                  | 0.000012 |
| closing tables                 | 0.000007 |
| logging slow query             | 0.000003 |
| logging slow query             | 0.000048 |
| cleaning up                    | 0.000006 |
+--------------------------------+----------+
20 rows in set (0.00 sec)
So you can see all the different operations performed by the server for that one query. This is invaluable in finding problems I would say in slow queries and bottlenecks in the database.
There is also a CPU profile you can use to see the CPU’s timings, and to do this, you just add on the CPU keyword:
SHOW PROFILE CPU FOR QUERY 1;
There are others you can use, MEMORY, BLOCK IO, SWAPS, etc. They are all available in the MySQL reference for the SHOW PROFILE syntax, http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html
Profiling was introduced in MySQL version 5.0.37 Community Server.
« go backkeep looking »
  • Chris Tate-Davies

    Hello there. This is my little "repository" on the world wide web. Its for nothing more than documenting things that I might need again in the future. You could describe it as an extension to my memory.

    Also an online collaboration of my thoughts through the day. I'll try to keep the real random stuff out and keep the blog on course.

    Thanks for stopping by... Hope you find what you're looking for...

  • Tags