lee的个人空间

    理论指导实践,实践验证理论

    正在浏览 Linux 里的文章

    对于扣订人员来说,用程序实现自动化流程处理是非常省事的,如同google的广告系统,它完全不需要人工干预,简直就是一部造钱的机器。

    让人蛋疼的是我所做的工作本是自动化的,近期由于合作方问题,导致数据不再符合自动化统计流程,领导有命:统计数据还是要形成常态出具的,悲催的我只能领命。

    对于大量的日志数据,虽不能全自动化,但也不至于完全靠手工,实现个半自动化统计程序应对短期的突发事故还是可行的

    日志格式以竖线分隔,需要统计出每个位置的点击数,需求比较简单,首先考虑的是利用shell来完成。

    可利用cut来截取所需字段,另shell script做数值运算也挺简单,但需要一个类似map的存储结构,位置作为key, 点击数作为value

    shell没有map类型数据结构,仅有的是array,查阅了相关array使用方法后,在特定场合可以将array充当map来使用——位置当作array的下标,该位置的点击数则充当array下标的值

    代码如下:

    #! /bin/bash
    #author sawenlee 2011-09-01
    #func   calculate the sum of each position
    #
    PATH=.:/usr/local/bin:/bin:/usr/bin:/usr/sbin/:/sbin:/usr/local/sbin:/home/dev/bin:/usr/local/mysql/bin
    export PATH
    base=/data/urlred
    #args length must be greater than 0
    if [ $# == "0" ]; then
    echo “Usage: [calc_click] src_file”
    exit 1
    fi
    declare -a result #we need an data structure to collect results
    declare -i num
    maxLen=100 #set array max length
    #this is the target file we are goting to handle
    txtFile=$base/$1
    if test -e $txtFile ; then
    echo “starting to handle file : $txtFile”
    echo “Index  count”
    for pos in `cat $txtFile | grep success| cut -d ‘|’ -f 4` ;  #截取位置元素
    do
    if [ $pos -lt $maxLen -a  $pos -gt 0 ]; then   #过滤掉无效的位置
    ((result[$pos]++))                                            #shell script的数值运算很特别
    fi
    done
    for  ((i = 1; i <= $maxLen; i++))                    #输出结果
    do
    if test ! -z “${result[$i]}”  ; then                    #如果有值才输出
    echo “$i ${result[$i]}”
    fi
    done
    else
    echo “$txtFile doesn’t exist”
    fi

     

     

    简要需求如下(注:以下域名和ip形式纯属假象,你肯定没见过a.b.c.d的ip,这些字母代表0-255之间的数字):

    1,aaa.com———>a.b.c.d:8080/web

    2,aaa.com/admin–>a.b.c.d:8082/admin

    3,bbb.com———->a.b.c.d:8083/wap

    经过多次的尝试,始终没法把第一条和第三条映射上,nginx的简化配置如下:

    server aaa.com

    location / {

    proxy_pass http://a.b.c.d:8080/web

    }

    location /admin{

    proxy_pass http://a.b.c.d:8082/admin

    }

    server bbb.com

    location /{

    proxy_pass http://a.b.c.d:8083/wap

    }

    # service nginx reconfigure后发现除了第二条映射生效外,其他两个都始终报302 404错误,然后将第一、三条 location / 分别改成 /web   /wap

    与/admin一致后,然后通过aaa.com/web  aaa.com/wap访问就能成功,通过这规律可能是一个域名只能映射到ip:port;而到模块名就必须也得在域名后加上相应的模块名;而且要用到模块名,在location处的映射必须与应用名一致即如果配置了下面这种形式同样会出错:

    server aaa.com

    location /myAdmin{

    proxy_pass http://a.b.c.d:8082/admin;

    }

    以上红色字体部分对某些应用必须要相同,否则没法跳转。

    根据现有情况,只能改变应用模块的访问方式了,将web,wap所有的应用名去掉,保证a.b.c.d:8080就能访问到web站点,而不需要加上/web,wap同理

    改动后自己服务器上测试后通过!

     

    nginx安装:http://www.javali.org/360

    一,top -b

    场景: 将java相关的日志每隔5分钟输出到制定的文件

    top -b -n1 -M |grep java >> /home/dev/logs/top.log

    -b : Batch mode operation
    Starts top in ‘Batch mode’, which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you’ve set with the ‘-n’ command-line option or until killed.
    -b参数就是为输出到文件或者其他程序而设计的。
    否则加入crontab 会报错。TERM environment variable not set

    -b : Batch mode operation

    Starts top in ‘Batch mode’, which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you’ve set with the ‘-n’ command-line option or until killed.

    -b参数就是为输出到文件或者其他程序而设计的。

    否则加入crontab 会报错。TERM environment variable not set

    二,删除特殊符号文件

    删除特殊文件

    rm — \\  这样就能删除了\的文件了

    rm — -ee 这样能删除带-的文件

    三 ,使用Screen在一个Shell里查看多个web服务的路径

    screen 使用

    * ctrl-a c:创建一个新的 Shell

    * ctrl-a ctrl-a:在 Shell 间切换

    * ctrl-a n:切换到下一个 Shell

    * ctrl-a p:切换到上一个 Shell

    * ctrl-a 0…9:同样是切换各个 Shell

    * ctrl-a d:退出 Screen 会话

    只需输入 screen -r

    运用:

    在shell里运行screen

    然后ctrl+a c新建一个screen 看需要重复操作

    然后ctrl+a 0-9可以切换  或者ctrl+a n p进行上下切换

    screen -ls列出 已存在的screen

    screen -r sessionId 即可恢复

    [@zw_76_31 ~]$ screen -d 24437

    [24437.pts-1.zw_76_31 detached.]

    四,linux添加交换分区

    dd if=/dev/zero of=/home/swap bs=1024 count=4096000

    mkswap /home/swap

    swapon /home/swap

    1

    五 ,磁盘空间查看

    1、df 磁盘空间使用情况

    df -h

    2、du检查该路径下的每个文件夹所占空间大小,max-depth表示只显示第一集目录,这样便于查看。

    du –max-depth=1

    3、lsof检查是否有文件被删除,而进程还活着

    lsof |grep delete

    六 、查找进程PID
    pid=`ps -ef | grep tomcat$i |grep -v ‘grep’ | awk ‘{print $2}’`

    七、端口转发

    nohup ssh -g -L 8080:localhost:3306 192.168.1.7

    将本机的8080端口上的数据转发到192.168.1.7的3306端口上

    如果localhost有公网IP映射,而192.168.1.7没有,要在公网访问192.168.1.7的mysql 服务,通过这种端口数据转发,绝对必杀技!!

    八、求和

    数据格式 2011080215|8|1|1 要求第三列和

    cat *.txt | cut -d ‘|’ -f 3 | awk ‘{sum+=$0}END{print sum}’

     

    习惯了Linux的用户刚开始接触AIX第一个不能适应的肯定是shell,大多linux版本默认用的bash,命令自动补全,上下按键,backspace键在Aix下全不好用,这是因为AIX默认用的是ksh,在这个shell下不支持bash下的一些操作
    还好shell不就一个“壳”么,给换了不就行了

    第一步,检测是否已经安装了bash
    #rpm -qa | grep bash
    如果没有内容则说明没有安装

    第二步,下载bash for AIX
    http://www-03.ibm.com/systems/power/software/aix/linux/toolbox/alpha.html
    事先看好AIX系统的版本,然后找到相应的bash 的AIX版,我用的是5.3,所以我下的文件名是: bash-3.2-1.aix5.2.ppc.rpm

    第三步,安装bash
    #rpm -ivh  bash-3.2-1.aix5.2.ppc.rpm
    等待完成即安装好了
    #rpm -qa | grep bash 执行了有内容的话就说明已经安装成功了

    第四步,换shell
    #vi /etc/passwd
    将root默认shell由/usr/bin/ksh改为/usr/bin/bash
    修改完保存时只要:wq!强制保存就可以了
    修改完退出终端重新开一个是不是linux下熟悉的shell出现了
    另外一种是:
    #usermod -s /usr/bin/bash u1
    建议用后者

    另外设置一下命令提示符风格:
    #vi ~/.profile
    在最后加上export PS1=’[\u@\h \w] \$ ‘
    具体含义自己baidu去
    这样linux下完整的bash就出来了

    Linux操作系统下, ftp服务对于一般用户来说是一多余的服务,完全可以用ssh替代, 但是对于需要大量共享文件的服务来说,还是很实用的,vsftpd是very secure的,也就是加密传输,安全性还不错,在linux上用它来搭建ftp server一般是首选;

    vsftpd有关的配置文件:  /etc/vsftpd/vsftpd.conf 要搭建符合个人需求的ftpserver,了解这个文件的配置就足够了

    服务端设置:
    1,搭建一个匿名用户就可以登录的ftp
    只需要把anonymous_enable=YES  默认就允许登录
    匿名用户有写权限配置需要下面几项:
    write_enable=YES
    anon_other_write_enable=YES
    anon_mkdir_enable=YES
    anon_mkdir_write_enable=YES
    anon_upload_enable=YES
    匿名用户写权限默认是没有的,需要加上去,不过这样的权限设置比较有风险,需要三思!!!


    2,允许本地用户登录并且只能限定在家目录
    在操作这步时,出了点问题:  我有一个用户 sawen  家目录在/home/sawen
    按照默认的配置是允许本地用户访问ftp server的,但是我在登录时候出现这个错误:

    [root@bogon vsftpd]#  ftp localhost
    Connected to localhost.localdomain.
    220 (vsFTPd 2.0.5)
    530 Please login with USER and PASS.
    530 Please login with USER and PASS.
    KERBEROS_V4 rejected as an authentication type
    Name (localhost:root): sawen
    331 Please specify the password.
    Password:
    500 OOPS: cannot change directory:/home/sawen
    Login failed.


    后来查资料,发现家目录在/home分区的用户都有这个问题 ,我另添加一个用户家目录在/tmp分区
    useradd  -d /tmp/myftp myftp
    passwd myftp
    这样登录就可以了。。


    如果要限定 myftp用户只能在/tmp/myftp下操作,不能切换到其他分区,需要做如下设置
    # You may specify an explicit list of local users to chroot() to their home
    # directory. If chroot_local_user is YES, then this list becomes a list of
    # users to NOT chroot().
    chroot_list_enable=YES
    # (default follows)
    chroot_list_file=/etc/vsftpd/chroot_list

    把这两行注释的#去掉,
    然后新建 /etc/vsftpd/chroot_list   编辑这个文件 将需要限定的用户放这个文件里,一行一个;

    重启服务看看效果

    囧的事情是: 加入chroot_list的用户 不能登录成功,这个还没发现原因,要有知道的email告诉我!!!



    =========================================================华丽的分界线
    下面介绍几个有意思的参数:
    1),登录ftp后,欢迎语设置:
    banner_file=/etc/vsftpd/welcome.txt
    新建这个文件并写入欢迎语即可、
    ftpd_banner=Welcome to blah FTP service.  
    直接写文本
    当连接上主机的时候就会显示:  比如ftp localhost  看看;

    2,  限制下载带宽—限制所有的用户传输带宽最高只能是100Kb/s
    local_max_rate=100000这个我自己没试
    3,限制最大在线人数:
    max_clients=10
    4,同一IP连接数:
    max_per_ip=1
    超过1个链接会提示:
    Connected to localhost.localdomain.
    421 There are too many connections from your internet address.




    客户端连接:
    直接输入ftp命令  —》 open localhost/server ip    —》 允许匿名的话 直接输入anonymous既可以ftp身份登录了,不需要密码验证 ;   可以通过cd 切换目录   get 获取文件 put 上传文件 。
    直接输入? 或者help会罗列出所有客户端命令,这里就不一一介绍了,自个儿慢慢熟悉吧:)



    =======================================================================
    多套方案实现:等待!
    1,所有匿名用户可以下载
    2,所有匿名用户可以上传但不能下载
    3,所有本地用户都能登录
    4,  限制user1只能在家目录操作
    5,默认所有用户都只能在家目录,=而user1是信任用户,可以访问家目录外的其他资源

    环境: RedHat3.4 Enterprise Edition


    1,确认vncserver是否安装
    [root@localhost tmp]# rpm -qa | grep -i vnc
    vnc-server-4.0-11.el4
    如果没有出现内容,则自己需要下载rpm包先安装

    ====================================
    z这是额外加上的,后来在自己机器上实践,发现漏了点东西;
    第一次 启动vncserver 会出现 no displays configured
    这是要求你 先运行  命令  vncserver来设置密码  他会生成
    [test@xok.la ~]$ vncserver

    You will require a password to access your desktops.

    Password:123456
    Verify:123456
    xauth:  creating new authority file /root/.vnc/.Xauthority

    New ‘xok.la.localdomain:1 (test)’ desktop is xok.la.localdomain:1

    Creating default startup script /root/.vnc/xstartup
    Starting applications specified in /root/.vnc/xstartup
    Log file is /root/.vnc/xok.la.localdomain:1.log
    以上 只是截取网上的一段,实际个人是用root执行的
    ===============================================


    2,修改vnc配置

    cd /root/.vnc
    vi xstartup 
    #twm &  将默认的这一行注释
    gnome-session &  最后新加这一行

    这步是将窗口管理器由twm改成gnome 后者图形化界面用户更熟悉

    3,启动VNC服务

    检查vnc是否启动
    service vncserver status    或者 /etc/init.d/vncserver status  

    [root@localhost tmp]# service vncserver status
    Xvnc (pid 4886) is running…

    注意启动或停止vncserver不能用他提供的脚本 /etc/init.d/vncserver start /stop虽然会有提示,但是不会启动/停止成功! 这个脚本有问题,我的系统是redhat3.4,不知道其他系统有没有这个bug

    如果没有启动则需要 敲入 /usr/bin/vncserver :1   这里注意 :1 启动服务端口为 5901  :2- :5 启动5902-5905
    启动 :1即可

    如果已经启动,则需要重启server  使设置生效,vncserver -kill :1
    /usr/bin/vncserver :1 
    如果这里出现启动不成功 注意将/tmp下.X1-lock和文件夹 .X11-unix 删掉

    启动成功后再确认一遍service vncserver status    或者 /etc/init.d/vncserver status  


    4,客户端连接
    我用的是vncclient windows系统;打开后输入serverip:1  输入密码即可连接 ,是不是启动gnome桌面了



    当然这过程中还需要考虑的问题是中间防火墙是否允许5901-5905端口对外开放,并且需要root权限