BlogJava-kelefa-随笔分类-开源应用http://www.blogjava.net/kelefa/category/12607.html大千世界中,唯一缺乏的就是人类的注意力。zh-cnTue, 27 Feb 2007 12:04:42 GMTTue, 27 Feb 2007 12:04:42 GMT60网络设备主动告警系统之snmp告警的实现http://www.blogjava.net/kelefa/archive/2006/12/27/90225.html杨杰荣杨杰荣Wed, 27 Dec 2006 01:50:00 GMThttp://www.blogjava.net/kelefa/archive/2006/12/27/90225.htmlhttp://www.blogjava.net/kelefa/comments/90225.htmlhttp://www.blogjava.net/kelefa/archive/2006/12/27/90225.html#Feedback1http://www.blogjava.net/kelefa/comments/commentRss/90225.htmlhttp://www.blogjava.net/kelefa/services/trackbacks/90225.html阅读全文

杨杰荣 2006-12-27 09:50 发表评论
]]>
在java程序里telnet到远端设备执行命令http://www.blogjava.net/kelefa/archive/2006/06/30/56056.html杨杰荣杨杰荣Fri, 30 Jun 2006 15:52:00 GMThttp://www.blogjava.net/kelefa/archive/2006/06/30/56056.htmlhttp://www.blogjava.net/kelefa/comments/56056.htmlhttp://www.blogjava.net/kelefa/archive/2006/06/30/56056.html#Feedback1http://www.blogjava.net/kelefa/comments/commentRss/56056.htmlhttp://www.blogjava.net/kelefa/services/trackbacks/56056.html
        为了保住“高手”的称号,他又搞了一个监控该系统的小程序,能及时发现系统是否工作正常,如果发现异常情况立刻发送email给自己,email又跟自己的手机短信绑定,使他能迅速的发现情况并跑到电脑前:
//telnet进来后
[root@kelefa root]$ su -
[root@kelefa root]$ password 
[root@kelefa root]# cd 
/usr/local/resin-3.0.14/bin
[root@kelefa bin]# ./httpd.sh  stop
[root@kelefa bin]# .
/httpd.sh  start

一切又正常了,1分钟内搞定,一流程序员再次得到验证,可是email有时会发送失败,sp的短信也不是绝对的正常,而且每个月还给n元大洋给sp,万一收到告警短信旁边没有电脑也无能为力。

        终于发现更好的方法:在监控程序里直接telnet到设备上进行操作,动手前当然要看看有什么开源的可以利用,它就是Jakarta的Commons Net,这个包支持ftp,telnet,pop3,smtp,nntp等协议。

        首先实现一个telnet的基类,它有个connectAndDocommad()的方法,实现链接到指定的设备上,并启动一个线程执行一个模板方法(某设计模式):


   // 登陆
   log.debug( "login................" );
   if ( false == login() )
   {
    String msg = new Date().toString() + " telnet登陆不成功,ip:" + remoteip;
    log.warn( msg );
    SyslogUtil.send( msg );
    return;
   }

   log.debug( "doCommand................" );
   // 抽象方法
   doCommand();

   // 退出
   log.debug( "exit................" );
   exit();


        其次类RestartTask继承该抽象类,实现doCommand()抽象方法,往输出流发送操作命令,实现重启:

@Override protected void doCommand()
 {
  log.debug( "resin restart................" );
  try
  {
     writeLine("su -");    
     writeLine("password"); 
     writeLine("cd /usr/local/resin-3.0.14/bin");
     writeLine("./httpd.sh  stop");
     writeLine("./httpd.sh  start");

     readResult();
  }
  catch ( IOException e )
  {
   log.warn( e.getMessage() );
  }
 }


以上为伪代码,需要实现一些子方法, 如有雷同,纯属虚构!

杨杰荣 2006-06-30 23:52 发表评论
]]>
OSCache工具类http://www.blogjava.net/kelefa/archive/2006/06/29/OSCache.html杨杰荣杨杰荣Thu, 29 Jun 2006 01:32:00 GMThttp://www.blogjava.net/kelefa/archive/2006/06/29/OSCache.htmlhttp://www.blogjava.net/kelefa/comments/55678.htmlhttp://www.blogjava.net/kelefa/archive/2006/06/29/OSCache.html#Feedback2http://www.blogjava.net/kelefa/comments/commentRss/55678.htmlhttp://www.blogjava.net/kelefa/services/trackbacks/55678.htmlOSCache是OpenSymphony组织提供的一个J2EE架构中Web应用层的缓存技术实现方案,可以使用内存、硬盘空间、同时使用内存和硬盘作为缓存区灵活的缓存系统:OSCache支持多种缓存级别,使用相当灵活简单,在jsp中刷新缓存只要两行代码:


<% @ taglib uri = " oscache "  prefix = " cache " %>

< cache:flush group = " device_types "  scope = " application "   />

但是有时需要在java代码中刷新缓存,以下这个OSCacheUtil类可以工作,但是只能在webwork环境内调用:
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;
import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.webwork.ServletActionContext;

/**
 * osCache缓存工具类.
 * 只能在webwork环境内调用
 *
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * 
@author 杨杰荣
 * 
@version 1.0
 
*/

public class OSCacheUtil
{
  
private static final Logger log = Logger.getLogger( OSCacheUtil.class );

  
public static final int ALL_SCOPE = 0;
  
public static final int SESSION_SCOPE = PageContext.SESSION_SCOPE;
  
public static final int APPLICATION_SCOPE = PageContext.APPLICATION_SCOPE;

  
private static ServletCacheAdministrator admin = null;

  
private OSCacheUtil()
  
{
  }


  
/**
   * 刷新osCache组
   * 
@param group Cache组名
   * 
@param cacheScope Cache范围,只能是SESSION_SCOPE或APPLICATION_SCOPE
   
*/

  
public static void flushGroup( String group, int cacheScope )
  
{
    initCacheAdmin();

    
if ( cacheScope == SESSION_SCOPE || cacheScope == APPLICATION_SCOPE )
    
{
      Cache cache 
= admin.getCache( ServletActionContext.getRequest(),
                                    cacheScope );
      cache.flushGroup( group );
    }

    
else
    
{
      log.warn( 
"A cache group was specified for flushing, but the scope wasn't supplied or was invalid" );
    }

  }


  
/**
   * 刷新osCache中的某个key'
   * 
@param key String
   * 
@param cacheScope Cache范围,只能是SESSION_SCOPE或APPLICATION_SCOPE
   
*/

  
public static void flushKey( String key, int cacheScope )
  
{
    initCacheAdmin();

    
if ( cacheScope == SESSION_SCOPE || cacheScope == APPLICATION_SCOPE )
    
{
      String actualKey 
= admin.generateEntryKey(
          key, ServletActionContext.getRequest(), cacheScope, 
null );

      Cache cache 
= admin.getCache( ServletActionContext.getRequest(), cacheScope );
      cache.flushEntry( actualKey );
    }

    
else
    
{
      log.warn( 
"A cache key was specified for flushing, but the scope wasn't supplied or was invalid" );
    }

  }


  
/**
   * 刷新所有的osCache
   * 
@param cacheScope Cache范围,可以是SESSION_SCOPE,APPLICATION_SCOPE,ALL_SCOPE
   
*/

  
public static void flushAll( int cacheScope )
  
{
    initCacheAdmin();

    
if ( cacheScope == SESSION_SCOPE || cacheScope == APPLICATION_SCOPE )
    
{
      admin.setFlushTime( cacheScope );
    }

    
else
    
{
      admin.flushAll();
    }

  }



  
private static void initCacheAdmin()
  
{
    
if ( admin == null )
    
{
      admin 
= ServletCacheAdministrator.getInstance( ServletActionContext.
          getServletContext() );
    }

  }

}


杨杰荣 2006-06-29 09:32 发表评论
]]>