MSSQL - Finding the database restore history

7. August 2012 21:12

 

Did you know you can view the database restore history from MSSQL server. The following SQL will provide a list of all database restores that have been performed on a server at some point.

 

 

SELECT 
	destination_database_name, server_name AS 'SourceServer', database_name AS 'SourceDB', physical_device_name, type, backup_start_date, restore_date
	FROM msdb.dbo.restorehistory AS rh
		INNER JOIN msdb.dbo.backupset AS bs ON bs.backup_set_id = rh.backup_set_id
		INNER JOIN msdb.dbo.backupmediafamily AS bmf ON bmf.media_set_id = bs.media_set_id
	ORDER BY restore_date DESC;

 

 

It will produce output that looks like this.

 

 

Stev	DC01	Stev	E:\MSSQL-Backup\Stev.bak	D	2011-09-17 08:43:58.000	2011-09-17 09:10:25.283

 

E-mail Kick it! DZone it! del.icio.us Permalink


Linux - Killing all processes for a specific user

13. July 2012 08:00

 

Here is a few methods for killing tasks for a specific user in linux which may be required during account deletion or because somebody has managed to be an idiot and locked himself out with a fork bomb or some such.

 

The simple method is to use the utility called 'slay' which for debian / unbuntu and most other distrobutions is avilable if its not install you can install it using 'apt-get install slay'

 

It is very simple to use. Just running the command slay <username> and it will kill all of that users processes.

 

 

The other method to use when slay is not avilable is a combination of ps and kill. You can use the following command

 

 

kill -TERM `ps h --User nobody -o pid`

 

 

Understanding the above can other advantages as well because you can switch out the --User for --Group and kill processes by group id as well as for a specific user.

E-mail Kick it! DZone it! del.icio.us Permalink


Cisco - Howto enable scp file transfer

7. July 2012 11:16

 

By default cisco routers will come with scp file transfers disabled. Though they are easy enough to enable so that you can download / upload configurations to them using ssh / scp. Which is useful for doing backup's of router config files. In particular a lot of routers automatically.

 

The first part of the problem requires that you enable enough privalages to be able to access scp from a particular user. Normally there is a configuration line that looks like 'aaa authentication login default local' or like 'aaa authentication login default group radius local' if you are using radius authentication. You will need to add the exec permission to the same group.

 

you can do this with the following command

 

aaa authorization exec default  local

Or if using radius

aaa authorization exec default group radius local

 

 

Then you need to also enable the scp service which you can do with the following

 

 

ip scp server enable

 

 

At this stage you should now be able to scp the config file. From linux I would use the following 'scp <routerip>:startup-config mybackupfile

E-mail Kick it! DZone it! del.icio.us Permalink


boost python and custom exception

6. July 2012 08:00

 

One of the problems faced with creating boost python wrappers is that sooner or later you can run into the situation where you need to support extra exception messages which are being thrown from C++ code but are not understood by python or the python wrapper. So this is a guide to support these extra exceptions.

 

You will know that you have come across this issue when you see the following being produced in python. In this case the Example function in C++ throws a MyException class which provides an error message which in its current state just isn't really useful to try to trace the problem.

 

 

>>> import MyClass_py
>>> obj = MyClass_py.MyClass()
>>> obj.Example()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: unidentifiable C++ exception
>>>

 

 

In previous posts about creating python wrappers and supporting operator overloading. I have been using much the same example code. In this case it has been changed to add the extra exception. So the C++ code now looks like this.

 

 

class MyException {
	public:
		MyException(const std::string str) {
			m_str = str;
		}
		
		~MyException() {
		
		}
		
		std::string GetMessage() const {
			return m_str;
		}
		
	private:
		std::string m_str;
};

class MyClass {
	public:
		MyClass() { }
		~MyClass() { }
		
		std::string Example() {
			throw(MyException("Hello World"));
		}
		
};

 

 

To get the exception method supported we need to add an exception converter to our python module using a boost python macro register_exception_translator and an extra C/C++ function in our wrapper. Note that the C++ function should not be in the BOOST_PYTHON_MODULE section but the register_exception_translator is. This is how it should look to handle the exception.

Also take note that because the MyExceptionTranslator requires the argument to be a const the functions that you call must also be marked as const or it will not compile. In this case the function for "MyException::GetMessage" must be declared as "std::string MyException::GetMessage() const" to say that it does not modify the contents of the MyException this is because it is meant to be constant.

 

static void MyExceptionTranslator(const MyException &err) {
        PyErr_SetString(PyExc_UserWarning, err.GetMessage().c_str());
}


BOOST_PYTHON_MODULE(MyClass_py)
{
	register_exception_translator<MyException>(&MyExceptionTranslator);

	class_<MyClass>( "MyClass", init<>() )
	
		.def("Example", static_cast< std::string (MyClass::*)() > (&MyClass::Example) )
		
	;
	
}

 

When compiled and run from python we now get the following instead of the useless error message above

 

 

>>> import MyClass_py
>>> obj = MyClass_py.MyClass()
>>> obj.Example()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UserWarning: Hello World

 

I have included a full working example.

 

#include <string>

#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/init.hpp>
#include <boost/python/operators.hpp>
#include <boost/python/call_method.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/implicit.hpp>
#include <boost/python/return_by_value.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/overloads.hpp>
#include <boost/ref.hpp>
#include <boost/utility.hpp>

using namespace boost::python;


class MyException {
	public:
		MyException(const std::string str) {
			m_str = str;
		}
		
		~MyException() {
		
		}
		
		std::string GetMessage() const {
			return m_str;
		}
		
	private:
		std::string m_str;
};

class MyClass {
	public:
		MyClass() { }
		~MyClass() { }
		
		std::string Example() {
			throw(MyException("Hello World"));
		}
		
};

static void MyExceptionTranslator(const MyException &err) {
        PyErr_SetString(PyExc_UserWarning, err.GetMessage().c_str());
}


BOOST_PYTHON_MODULE(MyClass_py)
{
	register_exception_translator<MyException>(&MyExceptionTranslator);

	class_<MyClass>( "MyClass", init<>() )
	
		.def("Example", static_cast< std::string (MyClass::*)() > (&MyClass::Example) )
		
	;
	
}

The above example can be compiled with

 

g++ -shared -Wall MyClass_py.cpp -o MyClass_py.so -I/usr/include/python2.6/ -lboost_python -lpython2.6

 

E-mail Kick it! DZone it! del.icio.us Permalink


boost python operator overloading for calling C++ classes

3. July 2012 08:00

 

In a previous post about creating basic python wrappers. I mentioned that there can be an issue with trying to call C++ from the python wrapper which are operator overloaded. Taking the following C++ code the python wrapper cannot tell the different between the two functions when being compile or which one should be used.

 

class MyClass {
	public:
		MyClass() { }
		~MyClass() { }
		
		std::string Example() {
			return "Hello World";
		}
		
		std::string Example(std::string str) {
			return str;
		}
};

BOOST_PYTHON_MODULE(MyClass_py)
{

	class_( "MyClass", init<>() )
	
		.def("Example", &MyClass::Example)
		
	;
	
}

When compiling you will get en error of this.

 

MyClass_py.cpp: In function 'void init_module_MyClass_py()':
MyClass_py.cpp:44: error: no matching function for call to 'boost::python::class_<MyClass, boost::python::detail::not_specified, boost::python::detail::not_specified, boost::python::detail::not_specified>::def(const char [8], <unresolved overloaded function type>)'

 

You can work around it using the static cast to get the correct function to be called and rename the python function to something else. Effectivly this removes the operating overload from the python end of things when it is not supported. The example of the wrapper code is below.

 

BOOST_PYTHON_MODULE(MyClass_py)
{

	class_<MyClass>( "MyClass", init<>() )
	
		.def("Example", static_cast< std::string (MyClass::*)() > (&MyClass::Example) )
		.def("ExampleStr", static_cast< std::string (MyClass::*)(std::string ) > (&MyClass::Example) )
		
	;
	
}

Our python code is then forced to use the aliased function name.

 

>>> import MyClass_py
>>> obj = MyClass_py.MyClass()
>>> obj.Example()
'Hello World'
>>> obj.ExampleStr('Foo')
'Foo'
>>>

I have included an complete example for you to play with.

 

#include <string>

#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/init.hpp>
#include <boost/python/operators.hpp>
#include <boost/python/call_method.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/implicit.hpp>
#include <boost/python/return_by_value.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/overloads.hpp>
#include <boost/ref.hpp>
#include <boost/utility.hpp>

using namespace boost::python;

class MyClass {
	public:
		MyClass() { }
		~MyClass() { }
		
		std::string Example() {
			return "Hello World";
		}
		
		std::string Example(std::string str) {
			return str;
		}
};


BOOST_PYTHON_MODULE(MyClass_py)
{

	class_<MyClass>( "MyClass", init<>() )
	
		.def("Example", static_cast< std::string (MyClass::*)() > (&MyClass::Example) )
		.def("ExampleStr", static_cast< std::string (MyClass::*)(std::string ) > (&MyClass::Example) )
		
	;
	
}

 

You can compile the above with

 

g++ -shared -Wall MyClass_py.cpp -o MyClass_py.so -I/usr/include/python2.6/ -lboost_python -lpython2.6

E-mail Kick it! DZone it! del.icio.us Permalink