查看Linux网络连接及连接总数


netstat -an | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

查看系统tcp连接中各个状态的连接数。

netstat -an |grep 80 |grep ESTA |awk '{print$5 "\n"}' |awk 'BEGIN {FS=":"} {print $1 "\n"}'
 |sort |uniq

查看和本机80端口建立连接并状态在established的所有ip

netstat -n | awk '/^tcp/ {n=split($(NF-1),array,":");
if(n<=2)++S[array[(1)]];else++S[array[(4)]];++s[$NF];++N}
 END {for(a in S){printf("%-20s %s\n", a, S[a]);++I}
 printf("%-20s %s\n","TOTAL_IP",I);for(a in s)
printf("%-20s %s\n",a, s[a]);printf("%-20s %s\n","TOTAL_LINK",N);}'

输出每个ip的连接数,以及总的各个状态的连接数


Linux上使用php成功链接Microsoft SQL Server


  1. 安装PHP的SQL Server驱动
yum search freetds
yum install freetds php-mssql

或者下载编译安装

[root@server ~]# wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
[root@server ~]# tar -zxvf freetds-stable.tgz
[root@server ~]# ./configure
[root@server ~]# make
[root@server ~]# make install

2.配置FreeTDS
打开 FreeTDS 配置文件(/etc/freetds.conf,如果是编译安装可能在其他位置),在配置文件末尾可以看到一些简单的配置案例,
[mybigserver]
host = dbserver.example.org
port = 1433
tds version = 7.0

"mybigserver" 是你的服务器的简称标识, 随便你怎么命名, 只要在使用的时候记得它就可以. host 和 port 就不用赘述了, 对于MS SQL SERVER , tds version 应该是 7.0 , 而 对于 Sybase ,tds version 应该是 5.0.

  1. 测试能否链接到服务器
#  ping 192.168.1.33
# telnet 192.168.1.33 1433
Trying 192.168.1.33...
Connected to 192.168.1.33.
Escape character is '^]'.
# tsql -H 192.168.1.33 -p 1433 -U my_mssql_username
locale is "en_US.UTF-8"
locale charset is "UTF-8"
Password: my_mssql_password
1> use <em>mydatabase</em>
2> select * from <em>mytable</em>
3> go
# tsql -S mybigserver -U my_mssql_username
locale is "en_US.UTF-8"
locale charset is "UTF-8"
Password: my_mssql_password

如果能得到上面的运行结果,说明freetds运行正常。

如果出现:

locale is "en_ZA.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Default database being set to DatabaseName
Error 20004 (severity 9):
    Read from the server failed
    OS error 104, "Connection reset by peer"
Error 20002 (severity 9):
    Adaptive Server connection failed
There was a problem connecting to the server

错误是由于使用了错误版本的TDS。
要解决TSQL命令指定TDS之前的版本:
TDSVER=8.0 tsql -H 192.168.1.33 -p 433 -U my_mssql_username

还有就是您的IP地址列入白名单由数据库服务器?如果没有,你将无法连接。

  1. 测试PHP是否正常链接
    如果以上步骤都没有问题,下面代码测试PHP是否能正常链接:
<?php
$server = 'mybigserver ';
$link = mssql_connect($server, 'sa', 'phpfi');
if (!$link) {
    die('Something went wrong while connecting to MSSQL');
}
?>

这里,如果直接根据文档说明,使用mssql_connect(xxx,xxx,xxx)会得到一个让人抓不着头脑的错误消息“mssql_connect(): Unable to connect to server",这里主要是因为Centos系统开启了selinux,并且selinux默认阻止apache发起远程链接,可以执行如下命令解决:
setsebool -P httpd_can_network_connect=1


libevent 编译出错记


libevent-2.0.12-stable 编译出错:

i686-pc-linux-gnu-gcc -O2 -march=i686 -pipe -g -Wall -I../include -I../include
-DVERSION=1.05 -g -L.  -o btlist btlist.c -lbt -lresolv -luuid -lssl -lcurl 
`curl-config --libs` -lm
In file included from btlist.c:16:
/usr/include/openssl/sha.h:109: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:111: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:116: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:118: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:138: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:140: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:142: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:144: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:185: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:187: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:189: error: expected declaration specifiers or '...'
before 'size_t'
/usr/include/openssl/sha.h:191: error: expected declaration specifiers or '...'
before 'size_t'

几经周折终于找到了答案:
在config.h 文件中添加一行 头文件的引用中:
#include <sys/types.h>
然后make clean之后再重新

./configure 和 make 就搞定了


js 读取本地图片


js 读取本地图片 并取得base64 数据:
<h3>获取图片</h3>
<input type="file" value="选择图片" data-clear-btn="true" name="file-2" id="imgOne" onchange="preImg(this.id,'imgPre');">

<input type="text" data-clear-btn="true" id="datatxt"></input>

<script>
function preImg(sourceId, targetId) {

    if (typeof FileReader === 'undefined') {
        alert('Your browser does not support FileReader...');
        return;
    }
    var reader = new FileReader();
    reader.onload = function(e) {
        var img = document.getElementById_x_x_x(targetId);
        img.src = this.result;
        var data = this.result;
        $("#datatxt").val(data.substr(23));
       // alert("dddd:"+this.result);
    }
  reader.readAsDataURL(document.getElementById_x_x_x(sourceId).files[0]);

}
</script>


Nginx 图片等资源负载均衡配置


我们在使用nginx 进行负载均衡的时候,有时候要对图版等资源也进行分流,做法如下:
主nginx 的server 下 配置 :
location ~* .(source|ico|css|js|gif|jpe?g|png|html5|manifest)$ {

            expires 30d;
            proxy_pass http://192.168.10.xxx:80;
            add_header Pragma public;
    }

从服务器,也是要资源所在的服务器:
server {

    listen       80;
    server_name  localhost 192.168.10.xxx;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    root /usr/web/html;
    location / {
        root   /usr/web/html;
        index  index.html index.htm;
    }
location ~* \.(source|ico|css|js|gif|jpe?g|png|html5|manifest)$ {
            expires 30d;
            add_header Pragma public;
    }

}

只要两台机器的Nginx重启就Ok了! 这样你的图片等资源都可以访问了!