Using SNMP for administ PogoPlug

###Preparation For using SNMP for administrating PogoPlug, we have to install following packages first:

	# apt-get install snmpd smartmontools super

snmpd is the daemon process for snmp, smartmontools is the monitor tools for harddisk, super let you run daemon process in super priviledge.
###Configuration Configure snmp firstly.

###Client Configuration Install the webserver using the following command:

	$ pacman -S apache php php-apache mariadb

After install the packages, if you want to change the configuration file, directly edit the /etc/httpd/conf/httpd.conf or extra/httpd-default.conf, then using systemd to restart the httpd daemon.
Change priviledge of the public_html directory:

	$ chmod o+x ~
	$ chmod o_x ~/public_html
	# A more safe way for controlling the safety
	# usermod -aG groupname http

###SSL Configuration Generate a new self-signed certificate(key size and the number of days of validity will be customized):

	# openssl genrsa -out server.key 2048
	Generating RSA private key, 2048 bit long modulus
	..........................+++
	...........................................................................................................................................................+++
	e is 65537 (0x10001)
	[root@XXXyyy conf]# ls
	extra  httpd.conf  magic  mime.types  server.key
	[root@XXXyyy conf]# chmod 600 server.key
	[root@XXXyyy conf]# openssl req -new -key server.key -out server.csr
	You are about to be asked to enter information that will be incorporated
	into your certificate request.
	What you are about to enter is what is called a Distinguished Name or a DN.
	There are quite a few fields but you can leave some blank
	For some fields there will be a default value,
	If you enter '.', the field will be left blank.
	-----
	Country Name (2 letter code) [AU]:
	State or Province Name (full name) [Some-State]:
	Locality Name (eg, city) []:
	Organization Name (eg, company) [Internet Widgits Pty Ltd]:
	Organizational Unit Name (eg, section) []:
	Common Name (e.g. server FQDN or YOUR name) []:
	Email Address []:
	
	Please enter the following 'extra' attributes
	to be sent with your certificate request
	A challenge password []:
	An optional company name []:
	[root@XXXyyy conf]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
	Signature ok
	subject=/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd
	Getting Private key
	[root@XXXyyy conf]# ls
	extra  httpd.conf  magic  mime.types  server.crt  server.csr  server.key

Then, uncomment the line containing following lines in /etc/httpd/conf/httpd.conf:

	Include conf/extra/httpd-ssl.conf
	Restart httpd to see the result. 

###Enable php Place Following line after LoadModule dir_module modules/mod_dir.so in /etc/httpd/conf/httpd.conf:

	LoadModule php5_module modules/libphp5.so
Place following line at the end of the Include list:
	Include conf/extra/php5_module.conf
Make sure you uncommented following line:
	TypesConfig conf/mime.types
	# Optional uncomment:
	MIMEMagicFile conf/magic
Add this line in /etc/httpd/conf/mime.types:
	application/x-httpd-php       php    php5
Make sure libphp5.so is under /etc/httpd/modules, if not, you may forgot to install php-apache.  
Test the php via following line under test.php:
	<?php phpinfo(); ?>

###Configure MariaDB Uncomment at least one line under /etc/php/php.ini:

	extension=pdo_mysql.so
	extension=mysqli.so
	extension=mysql.so

###Mysql Configuration

	[root@XXXyyy http]# mysqld_safe --skip-grant-tables &
	[1] 26315
	[root@XXXyyy http]# 131014 16:55:07 mysqld_safe Logging to '/var/lib/mysql/XXXyyy.err'.
	131014 16:55:07 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
	
	[root@XXXyyy http]# mysql -u root mysql
	Welcome to the MariaDB monitor.  Commands end with ; or \g.
	Your MariaDB connection id is 6
	Server version: 5.5.33a-MariaDB-log Source distribution
	
	Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
	
	Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
	
	MariaDB [mysql]> UPDATE mysql.user SET Password=PASSWORD('******') WHERE User='root';
	Query OK, 4 rows affected (0.00 sec)
	Rows matched: 4  Changed: 4  Warnings: 0
	
	MariaDB [mysql]> FLUSH PRIVILEGES;
	Query OK, 0 rows affected (0.00 sec)
	
	MariaDB [mysql]> exit
	Bye
	[root@XXXyyy http]# systemctl start mysqld

###CACTI configuration Install cacti via:

	$ pacman -S cacti
	$ pacman -S php-snmp

uncomment the following lines in /ec/php/php.ini:

	;extension=mysql.so
	;extension=sockets.so
	;extension=snmp.so
	open_basedir=...
	;date.timezone =

Start mysqld:

	$ systemctl enable mysqld.service
	$ systemctl start mysqld.service

Start snmpd:

	$ systemctl enable snmpd
	$ systemctl start snmpd

Change the configuration file /etc/httpd/conf/httpd.conf, inside <Directory “/srv/http"> block, ensure you have:

	AllowOverride All

Create the mysql based db:

	$ mysqladmin -u root -p create cacti
	Enter password: 

Generate the sql table using existing template:

	[root@XXXyyy http]# mysql -u root -p cacti < /usr/share/webapps/cacti/cacti.sql
	Enter password: 

Update the configuration of the database, you can replace the password from “some_password” to your own password.

	[root@XXXyyy http]# mysql -u root -p
	Enter password: 
	Welcome to the MariaDB monitor.  Commands end with ; or \g.
	Your MariaDB connection id is 10
	Server version: 5.5.33a-MariaDB-log Source distribution
	
	Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
	
	Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
	
	MariaDB [(none)]> GRANT ALL ON cacti.* TO cacti@localhost IDENTIFIED BY 'some_password';
	Query OK, 0 rows affected (0.00 sec)
	
	MariaDB [(none)]> FLUSH PRIVILEGES;
	Query OK, 0 rows affected (0.00 sec)
	
	MariaDB [(none)]> exit
	Bye

Change the username and password of the config.php of cacti:

	$ vim  /usr/share/webapps/cacti/include/config.php
	$ database_username = "cacti";
	$ database_password = "some_password";

Clean up the cacti directory:

	chown -R http:http /usr/share/webapps/cacti/{rra,log}
	rm /usr/share/webapps/cacti/.htaccess
	chmod +x /usr/share/webapps/cacti/{cmd,poller}.php /usr/share/webapps/cacti/lib/ping.php

Install caccti-spine, a fast poller for cacti, from AUR. Change the db name and db password to:

	$ vim /etc/spine.conf
	DB_User cacti
	DB_Pass some_password

Restart crontab:

	$ systemctl restart cronie.service
	$ systemctl restart httpd

How python interact with RaspberryPI GPIO

Yesterday I use RaspberryPI to control the LED using its GPIO port. with Python code we can directly “talk” to RaspberryPI, and set LED on and off. But what is the principle in it? How to realize our own library(for example, python/perl/lua based library?), here I will dive into the python library code and find what’s really inside the python code.

###Prepare the environment Install the RPi.GPIO via pip, $ pip install RPi,GPIO .
After installed the GPIO library, we can insert help(‘modules’) in python shell/prompt window to view all of the installed modules, we could found RPi in the list of the packages.

We can found the corresponding packages under the installation directory of python, so first you have to find the location of your python executable file, use “which python” to find the destination of the python.
For example, my RPi installed location is under: /home/Trusty/.virtualenvs/venv2/lib/python2.7/site-packages, not in the standard directory, because the python runs in the virtual environment.

###How to control the GPIO in UserSpace. Controlling GPIO in shell :

	root@rasp:~# echo 18 > /sys/class/gpio/export 
	root@rasp:~# ls /sys/class/gpio/
	export  gpio18  gpiochip0  unexport
	root@rasp:~# echo "out" > /sys/class/gpio/gpio18/direction 
	root@rasp:~# echo 1 > /sys/class/gpio/gpio18/value 
	root@rasp:~# 
	root@rasp:~# echo 23 > /sys/class/gpio/export 
	root@rasp:~# ls /sys/class/gpio/
	export  gpio18  gpio23  gpiochip0  unexport
	root@rasp:~# echo "out" >/sys/class/gpio/gpio23/direction 
	root@rasp:~# echo 1>/sys/class/gpio/gpio23/value 
	-bash: echo: write error: Invalid argument
	root@rasp:~# echo 1 > /sys/class/gpio/gpio23/value 
	root@rasp:~# echo 0 > /sys/class/gpio/gpio23/value 
	root@rasp:~# echo 0 > /sys/class/gpio/gpio18/value 

So we can see, the standard procedure is: write exported gpio number to export file–> write direction to gpioXX’s direction–> write value to the gpioxx’s value. The python script wrapped the upper procedures, so we directly call them in python written scripts.

xively入门(1)

###What is Xively?
Xively Cloud Services™
The world’s first IoT Public Cloud
###How to get start? Read tutorial and generate an API.
Then you can visit your own APP under: WEB TOOLS-> DEVELOP -> something.
Create a new Development Devices, named “Raspberry Pi”, xively will automatically generate the API Keys and the feed id for this developement device.

The key issue for using APIs is “talk” to the APIs and upload the data to the website. images/xively.jpg

软之妹纸

软之妹纸

从前有个软妹纸,她从小就立志要去寻找王纸,于是她骑着电动车出发了。 这天她来到一个国度,这个国度叫程序王国,这里的人个个都很悲伤,于是她问:“发生了什么让你们这么悲伤呢?” 人们告诉她:“我们的王纸被妖女抓走了,他是个帅哥,也很关心偶们,他被抓走后,我们都很伤心” 于是她去见老国王:“国王,我决定,去帮你救回王纸!” 白发苍苍的老国王,悲伤的抬起头:“软妹纸?靠你?我已经派了八队骑士去拯救王纸了,每次回来的都只有他们的马……” “放心吧,国王,我一定救回王纸”

于是软妹纸上路了……

她开着电动车走了七七四十九天,终于接近了妖女的城堡。路过城堡需要经过一座很长的石桥,把守石桥的是一只很凶恶的吃人龙。石桥上摆着被龙吃掉的森森白骨和一大堆盔甲,看来这都是国王派去的骑士们留下来的。 “我要把你吃掉!!!!” “那你试试看”软妹纸把电动车停好在桥头,双手叉腰站成个圆规仰着脖子看着龙 龙一张口,就把软妹纸吞到了肚子里。

故事就这么结束了吗?

木有!!!因为软妹纸是软的,软成什么程度呢,你们可以想象一下口香糖的嘛。她太不好消化了,龙被折腾的上窜下跳,终于受不了软妹纸的折磨,于是张开大口,放软妹纸出来了,并答应做软妹纸的坐骑。

于是软妹纸坐着龙飞过石桥到了城堡门口。她在城堡底下大声的喊“楼上有人吗?“ 王纸从最高一层的房间里探出头来,原来她被妖女关在了最高一层,每天要帮妖女改论文。这个房间有999米高,也没有梯子上去。城堡被施了魔法,龙没法起飞。

软妹纸肿么上去呢?故事就这么结束了吗?

木有!!! 软妹纸冲着上面喊:“王纸,把你的网线拔了扔下来,我攀上去!“ 然后王纸就把网线拔了仍了下来,软妹纸一路爬着上去了。

有个笨蛋说:王纸既然有网线,自己攀下来不就得了?—-亲,物理没学好对吧?王纸再苗条,体重也过了100了!知道啥叫百兆网线不!?就是能承受100斤载重的网线。俗话说,好女不过百,软妹纸就是这样一个“好女”。

天黑时候,妖女打完麻将回来了。她一进房间就恶狠狠的问“第四章改完了吗?我等着发表呢!“ “还没有,我的网线没有了,没法查资料!“ “那你就给老纸加班吧,晚上也不要吃饭了,赶紧写!“

“妖女,你太过分了,我要和你决斗!“软妹纸从窗帘后闪了出来 “你是谁?怎么上来的?“ “我叫软妹纸,网线没有,就是因为被我用来当攀爬绳了,放开那个王纸,他应该是自由的!“ “那好,尝尝我的三味白骨火吧,是人都熬不住的!“ 妖女话音刚落,口里就喷出三道火焰来,瞬间软妹纸就被烈焰包围了!

软妹纸死了吗?故事就那么结束了吗?

木有!!!木有!!!!!! 火烧了30分钟了,软妹纸笑语盈盈从火焰里走了出来。只用眼睛一闪,一道电光向妖女袭去,妖女惨叫一声,倒在地下。 “怎么可能?怎么可能?你居然一点事都没有?“妖女丧心病狂的叫喊着。 “没想到吧?我可是软妹纸哦!“ “在我临死前,告诉我为什么!?“ “好!” 软妹纸拿起自己的头发: “你看,离子烫的,防辐射,防高温,它保护了我的头部” 软妹纸指向自己的鞋子: “你看,UGG,加速效果20%,防高温防辐射防冰冻,你的烈焰一点用处都木有!” 软妹纸指着自己的袜子: “你看,黑丝,有效隔热、防寒、兼美化效果!” “至于胸部的保护,你没听过塑身内衣吗?当年黄蓉的软猬甲,也不过如此!!”

妖女大喊“我不相信,我不相信,至少,你的脸蛋还露在外面!!!还有手臂!” 软妹纸轻蔑的看着妖女: “手臂?防晒霜啊亲!SPI30的哦!” “脸蛋? 你不晓得现在的软妹纸都打粉底的嘛!我早就料到你的三味白骨火了,特地打了两斤粉才来的!唇膏是炫彩的,直接反射了热量” “最后告诉我,你是用什么招击败我的!?” “无敌电眼—-你看,我有这个。”软妹纸掏出一个小瓶:“这是睫毛膏”软妹纸又掏出一个小盒子:“这是无敌美瞳”“再加上假睫毛,电压高达三万伏特,所以只放了一次电你就倒了,能死在我无敌电眼下,妖女,你可以瞑目了!” 妖女缓缓的闭上了双眼……

王纸自由了,他再也不用替妖女改文章了!!! 就这样,软妹纸拯救了会写程序的王纸,他们骑着喷火龙一起回到了程序王国,从此过着幸福安宁的日子。

Crontab in ArchLinux

1. Install Crontab in ArchLinux

	$ pacman -S cronie

2. Enable the cronie at startup.

	$ systemctl enable cronie.service
	$ systemctl start cronie.service
	# Display the crontab jobs
	$ crontab -l

3. Edit the crontab jobs:

	MAILTO=your@email
	LOGFILE=/var/log/cron-pacman.log
	
	# 1. minute (0-59)
	# |   2. hour (0-23)
	# |   |   3. day of month (1-31)
	# |   |   |   4. month (1-12)
	# |   |   |   |   5. day of week (0-7: 0 or 7 is Sun, or use names)
	# |   |   |   |   |   6. commandline
	# |   |   |   |   |   |
	#min hr  dom mon dow command
	00   13   *   *   *  . /etc/profile && (echo; date; yes |pacman -Syuq) &>>$LOGFILE || (echo 'pacman failed!'; tail $LOGFILE; false)
	00   13   *   *   *  updatedb && date>>/root/done.txt

4. Now your job will be done automatically.