2017年3月

首页2017年3月
25
Mar
0

三种对话框

<scriptlanguage="javascript">

functionale()

{

//这个基本没有什么说的,就是弹出一个提醒的对话框

alert("我敢保证,你现在用的是演示一");

}

functionfirm()

{

//利用对话框返回的值(true或者false)

if(confirm("你确信要转去风亦飞的博客?"))

{

//如果是true,那么就把页面转向thcjp.cnblogs.com

location.href="http://blog.csdn.net/fengyifei11228/";

}

else

{

//否则说明下了,赫赫

alert("你按了取消,那就是返回false");

}

}

functionprom()

{

varname=prompt("请输入您的名字","");//将输入的内容赋给变量name,

//这里需要注意的是,prompt有两个参数,前面是提示的话,后面是当对话框出来后,在对话框里的默认值

if(name)//如果返回的有内容

{

alert("欢迎您:"+name)

}

}

24
Mar
0

Django 分页功能

官方示例 https://docs.djangoproject.com/en/1.9/topics/pagination/

from django.core.paginator import Paginator
objects = ['john', 'paul', 'george', 'ringo']
p = Paginator(objects, 2)

p.count

4

p.num_pages

2

type(p.page_range) # <type 'rangeiterator'> in Python 2.

<class 'range_iterator'>

p.page_range

range(1, 3)

page1 = p.page(1)
page1

<Page 1 of 2>

page1.object_list

['john', 'paul']

page2 = p.page(2)
page2.object_list

['george', 'ringo']

page2.has_next()

False

page2.has_previous()

True

page2.has_other_pages()

True

page2.next_page_number()

Traceback (most recent call last):
...
EmptyPage: That page contains no results

page2.previous_page_number()

1

page2.start_index() # The 1-based index of the first item on this page

3

page2.end_index() # The 1-based index of the last item on this page

4

p.page(0)

Traceback (most recent call last):
...
EmptyPage: That page number is less than 1

p.page(3)

Traceback (most recent call last):
...
EmptyPage: That page contains no results

view:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render

def listing(request):

contact_list = Contacts.objects.all()
paginator = Paginator(contact_list, 25) # Show 25 contacts per page

page = request.GET.get('page')
try:
    contacts = paginator.page(page)
except PageNotAnInteger:
    # If page is not an integer, deliver first page.
    contacts = paginator.page(1)
except EmptyPage:
    # If page is out of range (e.g. 9999), deliver last page of results.
    contacts = paginator.page(paginator.num_pages)

return render(request, 'list.html', {'contacts': contacts})

html:
{% for contact in contacts %}

{# Each "contact" is a Contact model object. #}
{{ contact.full_name|upper }}<br />
...

{% endfor %}

<div class="pagination">

<span class="step-links">
    {% if contacts.has_previous %}
        <a href="?page={{ contacts.previous_page_number }}">previous</a>
    {% endif %}

    <span class="current">
        Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
    </span>

    {% if contacts.has_next %}
        <a href="?page={{ contacts.next_page_number }}">next</a>
    {% endif %}
</span>

</div>

24
Mar
0

Django 的模板中的数学运算

前言
django模板只提供了加法的filter,没有提供专门的乘法和除法运算;
django提供了widthratio的tag用来计算比率,可以变相用于乘法和除法的计算。

加法
{{value|add:10}}
note:value=5,则结果返回15

减法
{{value|add:-10}}
note:value=5,则结果返回-5,加一个负数就是减法了

乘法
{% widthratio 5 1 100%}
note:等同于:(5 / 1) 100 ,结果返回500,withratio需要三个参数,它会使用参数1/参数2参数3的方式进行运算,进行乘法运算,使「参数2」=1

除法
{% widthratio 5 100 1%}
note:等同于:(5 / 100) * 1,则结果返回0.05,和乘法一样,使「参数3」= 1就是除法了。

21
Mar
0

ZABBIX安装方法

关于zabbix及相关服务软件版本:
Linux:redhat 6.5
nginx:1.9.15
MySQL:5.5.49
PHP:5.5.35
一、安装nginx:
安装依赖包:
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel
创建用户:
useradd nginx -s /sbin/nologin -M
下载nginx软件包并进入到目录中:
wget http://nginx.org/download/nginx-1.9.15.tar.gz && tar xvf nginx-1.9.15.tar.gz && cd nginx-1.9.15
编译:
./configure --prefix=/usr/local/product/nginx1.9.14 --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre
make && make install
ln -s /usr/local/product/nginx1.9.14 /usr/local/nginx ==>创建软链接
参数解释:
--with-http_stub_status_module:支持nginx状态查询
--with-http_ssl_module:支持https
--with-http_spdy_module:支持google的spdy,想了解请百度spdy,这个必须有ssl的支持
--with-pcre:为了支持rewrite重写功能,必须制定pcre

二、安装PHP
下载PHP安装包:
wget http://cn2.php.net/get/php-5.5.35.tar.gz/from/this/mirror
解压并编译:

mv mirror php-5.5.35.tar.gz && tar xvf php-5.5.35.tar.gz && cd php-5.5.35

./configure --prefix=/usr/local/product/php-5.5.35 --with-config-file-path=/usr/local/product/php-5.5.35/etc --with-bz2 --with-curl --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf --with-iconv-dir=/usr/local --enable-mbstring --enable-calendar --with-gettext --with-libxml-dir=/usr/local --with-zlib --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --enable-dom --enable-xml --enable-fpm --with-libdir=lib64 --enable-bcmath
提示“freetype-config not found” FreeType是一个完全开源的、可扩展、可定制且可移植的字体引擎,它提供TrueType字体驱动的实现统一的接口来访问多种字体格式文件
Yum -y install freetype*
再配置

make && make install
ln -s /usr/local/product/php-5.5.35 /usr/local/php

cp php.ini-production /usr/local/php/etc/php.ini
cd /usr/local/php/etc/

cp php-fpm.conf.default php-fpm.conf

修改php.ini参数:(zabbix环境需要修改的参数)
max_execution_time = 300
memory_limit = 128M
post_max_size = 16M
upload_max_filesize = 2M
max_input_time = 300
date.timezone = PRC

三、安装MySQL
添加mysql用户,创建mysql的数据目录:
groupadd mysql
mkdir -pv /data/mysql

useradd -r -g mysql -d /data/mysql -s /sbin/nologin mysql

chown -R mysql.mysql /data/mysql

安装cmake及依赖:
yum install cmake gcc* ncurses-devel -y

下载MySQL安装包:
wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.49.tar.gz

编译安装MySQL:

tar -xvf mysql-5.5.49.tar.gz && cd mysql-5.5.49

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/product/mysql5.5.49 -DDEFAULT_CHARSET=utf8 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql -DWITH_EXTRA_CHARSETS=all -DWITH_READLINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DDEFAULT_COLLATION=utf8_general_ci

make && make install

ln -s /usr/local/product/mysql5.5.49 /usr/local/mysql

chown -R mysql.mysql /usr/local/mysql

拷贝mysql的配置文件:

cd /usr/local/mysql/support-files/

cp my-medium.cnf /data/mysql/my.cnf

cp mysql.server /etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

初始化MySQL:
cd /usr/local/mysql/scripts

./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/

修改MySQL配置文件my.cnf中数据目录:

修改 /etc/my.cnf
datadir=/data/mysql/

启动MySQL:
[root@zabbix ~]# /etc/init.d/mysqld start
Starting MySQL... SUCCESS!

如果登录数据库出现问题:

[root@zabbix ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

解决办法:

[root@zabbix ~]# ln -s /tmp/mysql.sock /var/lib/mysql/

登录数据库,创建zabbix数据库及用户名和密码:

mysql> create database zabbix default charset utf8;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on zabbix.* to zabbix@'localhost' identified by 'zabbix';
Query OK, 0 rows affected (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
Database
information_schema
mysql
performance_schema
test
zabbix

5 rows in set (0.00 sec)

为数据库的root创建密码:
[root@zabbix]# mysqladmin -uroot password "zabbix"

四、安装zabbix server:
安装zabbix:
[root@zabbix ~]# tar zxf zabbix-3.0.3.tar.gz && cd zabbix-3.0.3

Yum -y install net-snmp-devel
编译zabbix:
./configure --prefix=/usr/local/zabbix-3.0.3/ --enable-server --enable-agent --with-mysql= /usr/local/product/mysql5.5.49/bin/mysql_config --with-net-snmp --with-libcurl --with-libxml2

make && make install

编译过程中如果有报错:

故障:
checking for mysql_config... no
configure: error: MySQL library not found

解决:
yum install mysql-devel -y

故障:
checking for net-snmp-config... no
configure: error: Invalid Net-SNMP directory - unable to find net-snmp-config

解决:
yum install net-snmp-devel -y

创建zabbix用户:
[root@zabbix zabbix-3.0.3]# groupadd zabbix

[root@zabbix zabbix-3.0.3]# useradd zabbix -s /sbin/nologin -M -g zabbix

zabbix server需要导入3个sql文件:

[root@zabbix zabbix-3.0.3]# mysql -uroot -pzabbix zabbix < database/mysql/schema.sql
[root@zabbix zabbix-3.0.3]# mysql -uroot -pzabbix zabbix < database/mysql/images.sql
[root@zabbix zabbix-3.0.3]# mysql -uroot -pzabbix zabbix < database/mysql/data.sql
如遇以下报错:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

解决办法:
Mkdir /var/lib/mysql
[root@zabbix ~]# ln -s /tmp/mysql.sock /var/lib/mysql/

五、zabbix管理网站配置(nginx):
创建项目目录:
[root@zabbix zabbix-3.0.3]# mkdir /data/web/zabbix.lifec.com -p
[root@zabbix zabbix-3.0.3]# mkdir /data/logs/zabbix -p

将前端文件拷贝到项目目录下:
[root@zabbix zabbix-3.0.3]# cp -rp frontends/php/* /data/web/zabbix.lifec.com/

编辑nginx虚拟主机:
cd /usr/local/nginx/conf/
[root@zabbix conf]# mkdir extra
[root@zabbix conf]# cd extra/
[root@zabbix extra]# vim zabbix.conf

server {
listen 8027;
server_name zabbix.lifec.com;
access_log /data/logs/zabbix/zabbix.lifec.com.access.log main;
index index.html index.php index.html;
root /data/web/zabbix.lifec.com;

location /{

   try_files $uri $uri/ /index.php?$args;

}

location ~ ^(.+.php)(.*)$ {

   fastcgi_split_path_info ^(.+.php)(.*)$;
   include fastcgi.conf;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   fastcgi_param PATH_INFO $fastcgi_path_info;

}

}

编辑nginx.conf配置文件:
[root@zabbix conf]# cat nginx.conf

user nginx;
worker_processes 1;

error_log logs/error.log warning;

error_log logs/error.log notice;

error_log logs/error.log info;

pid logs/nginx.pid;

events {

worker_connections  1024;

}

http {

include       mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;
include extra/*.conf;

}

编辑zabbix_server.conf文件:
[root@zabbix etc]# pwd
/usr/local/zabbix-3.0.2/etc/zabbix_server.conf
LogFile=/tmp/zabbix_server.log

PidFile=/tmp/zabbix_server.pid

DBHost=localhost

DBName=zabbix

DBUser=zabbix

DBPassword=zabbix

六、启动服务
启动nginx:
[root@zabbix conf]# /usr/local/nginx/sbin/nginx

启动PHP:
[root@zabbix conf]# /usr/local/php/sbin/php-fpm

启动zabbix server:

[root@zabbix conf]# /usr/local/zabbix-3.0.3/sbin/zabbix_server

如果启动的时候报错:
[root@zabbix ~]# /usr/local/zabbix-3.0.2/sbin/zabbix_server
/usr/local/zabbix-3.0.2/sbin/zabbix_server: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
[root@zabbix ~]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/

查看服务端口:

[root@zabbix conf]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1029/sshd
tcp 0 0 0.0.0.0:8027 0.0.0.0:* LISTEN 3730/nginx
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 3743/zabbix_server
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 3736/php-fpm
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 24922/mysqld
tcp 0 0 :::22 :::* LISTEN 1029/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 880/dhclient

将服务加入开机自启动:
[root@zabbix ~]# echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
[root@zabbix ~]# echo "/usr/local/php/sbin/php-fpm" >>/etc/rc.local
[root@zabbix ~]# echo "/etc/init.d/mysqld start" >>/etc/rc.local
[root@zabbix ~]# echo "/usr/local/zabbix-3.0.3/sbin/zabbix_server" >>/etc/rc.local

七、web端配置zabbix
页面访问
IP:8027

注:端口号3306

注:下载并上传完成后,修改权限然后直接点finish。

成功后会进入登录界面:
用户名密码:admin/zabbix

修改界面为中文界面
默认登录界面为英文:
修改以下配置,并重启服务即可;
vim /data/web/zabbix.lifec.com/include/locales.inc.php

  #'zh_CN' => ['name' => _('Chinese (zh_CN)'), 'display' => false],
  'zh_CN' => ['name' => _('Chinese (zh_CN)'), 'display' => true],

管理—》用户—》admin有语言的选择

为了防止图形中的字符出现乱码:
需要自己上传字体:

将选择的字体上传到Linux服务器的zabbix的fonts目录:
/data/web/zabbix.lifec.com/fonts

并修改以下配置:
vim /data/web/zabbix.lifec.com/include/defines.inc.php
45行define('ZBX_GRAPH_FONT_NAME', 'DejaVuSans'); // font file name
define('ZBX_GRAPH_FONT_NAME', 'simsun'); // font file name ==>此行为新增行;
define('ZBX_FONT_NAME', 'DejaVuSans');
define('ZBX_FONT_NAME', 'simsun'); ==>此行为新增行;

mv DejaVuSans.ttf DejaVuSans.ttf.bak
mv SIMSUN.TTC DejaVuSans.ttf

刷新页面查看

监控本机
修改配置文件
vim /usr/local/zabbix-3.0.3/etc/zabbix_agentd.conf
Server=127.0.0.1
ServerActive=127.0.0.1:10051
Hostname=localhost
UnsafeUserParameters=1 是否启用用户自定义监控脚本,1启用,0不启用
:wq保存并退出
执行
/usr/local/zabbix-3.0.3/sbin/zabbix_agentd
echo "/usr/local/zabbix-3.0.3/sbin/zabbix_agentd" >>/etc/rc.local
然后在页面上操作

启用后退出重新进一下,等2分钟出现如下图就表示zabbix已经可以监控自己了

替换监控图像上系统默认的字体 #默认字体不支持中文,如果不替换,图像上会显示乱码
在Windows系统中的C:WindowsFonts目录中复制出一个中文字体文件,例如msyh.ttf
把字体文件msyh.ttf上传到zabbix站点根目录下fonts文件夹中
例如:/usr/local/nginx/html/zabbix/fonts
备份默认的字体文件:DejaVusSans.ttf-bak
修改msyh.ttf名称为DejaVusSans.ttf
最后,修改配置信息,把默认语言修改为中文

21
Mar
0

课堂上同学写的LAMP安装脚本

!/bin/bash

echo the current directory is pwd
sourcedir=/usr/src
sourcefile=$sourcedir/basename ${1%%.tar*}
echo $sourcefile
echo $sourcedir
echo "########################################################"
if [ ${1##*.tar.} = gz ]
then

 echo -n  "uncompressed the $1 ......."
 tar zxvf $1 -C $sourcedir  >/dev/null 
 echo OK && cd $sourcefile 

elif [ ${1##*.tar.} = bz2 ]
then

 echo -n  "uncompressed the $1 ......."
 tar jxvf $1 -C $sourcedir  >/dev/null 
 echo OK && cd $sourcefile 

fi
echo "########################################################"
if [ pwd=$sourcefile -a ${1:0:5}=mysql ]
then
./configure --prefix=/usr/local/apache --enable-so --enable-rewrite&&
make &&
make install
elif [ pwd=$sourcefile -a ${1:0:4}=http ]
then

    groupadd -g 200  mysql&& useradd -u 200 -g mysql
    ./configure --prefix=/usr/loacl/mysql && make install
    cp -fr support-files/my-medium.cnf /etc/my.cnf
    cd /usr/local/mysql&& chown -R mysql:mysql . &&
    bin/mysql_install_db --user=mysql
    chown -R mysql var&&  bin/mysqld_safe --user=mysql &
    bin/mysqladmin -u root password '123456'

elif [ pwd=$sourcefile -a ${1:0:3}=php ]
then

 ./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache/bin/apxs  --with-mysql=/usr/local/mysql  --with-config-file-path=/usr/local/php5 ; make &&   make install 

cp php.ini-dist /usr/local/php5/php.ini
fi