26 Jun 2010

Down hết hình trên wallbase.net

#!/usr/bin/env python
import re, os
from urllib2 import urlopen
all_tag = []

def search_tag():
    index_page = 'http://wallbase.net'
    try:
        _page_ = urlopen(index_page)
        _html_ = _page_.readlines()
        _page_.close()
        p = re.compile(r'http://wallbase\.net/search/[a-z]+\"')
        for line in _html_:
            tag = p.findall(line)
            if tag:
                for i in tag:
                    all_tag.append(i[:-1a])
            else:
                continue
    except Exception, e:
        print "Connection Error!" + str(e)

def getsession(num):
    org_url = "http://wallbase.net/wallpaper/"
    second_url = org_url + num 
    _prefix = "http://wallpapers.yotoon.pl/rozne/"
    ext = '.jpg'
    fullname = "wallpaper-" + num + ext
    try:
        _page_ = urlopen(second_url)
        _html_ = _page_.readlines()
        _page_.close()
        for line in _html_:
            st = line.find(_prefix)
            nd = line.find(fullname)
            if st <> -1 and nd <> -1: 
                final = line[st:nd] + fullname
                print final
                os.system('wget ' + final)
            else:
                continue
    except Exception, e:
         print "Cannot connect to server!" + str(e)
         pass
                
def getnum(list_tag):
    for page in list_tag:
        try:
            response = urlopen(page)
            content = response.readlines()
            response.close()
            for line in content:
                a = line.find('thumb-')
                b = line.find('jpg') 
                if a <> -1 and b <> -1:
                    c = a + 6
                    d = b - 1
                    number= line[c:d]
                    getsession(number)
                    continue
                else:
                    continue
        except:
            print "May be we got wrong image url!"
        
if __name__ == '__main__':
    search_tag()
    getnum(all_tag)
exit()
Net chậm thì down chắc mất cả tháng :-D

9 Jun 2010

Nagios plugin

1st time I try to write plugin for nagios, it's really easy and simple with python :-). If you dont know how to use, please read this
First, check if we can reach website, then count response time and return nagios status.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Hung Nguyen Van
# 09-06-2010
from urllib2 import urlopen
import time, sys, socket

def timer(_dest):
    timeout = 10
    socket.setdefaulttimeout(timeout)    
    try:
        _response = urlopen(_dest)
    except:
        print "CRITICAL: Cannot connect to server!"
        sys.exit(3)
    start_time = time.time()
    _response = urlopen(_dest)
    end_time = time.time()
    final = end_time - start_time
    return final

if __name__ == '__main__':
    _dest = "https://" + sys.argv[1]
    seconds = timer(_dest)
    if seconds >= 10.0:
        print "CRITICAL: Server took %.2f seconds to response" % (seconds)
        sys.exit(2)
    if seconds >= 7.0:
        print "WARNING: Server took %.2f seconds to response" % (seconds)
        sys.exit(1)
    else:
        print "OK: Server took %.2f seconds to response" % (seconds)
        sys.exit(0)

Disqus