midrange.com code scratchpad
Name:
Syslog CMD and CPP
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
06/05/2009 10:39:03 pm
IP:
Logged
Description:
iSeries CMD and CPP source to send a syslog message.
Code:
  1. CMD        PROMPT('Send Syslog Message')                           
  2.                                                                                 
  3. PARM       KWD(RMTSYS) TYPE(*CHAR) LEN(128) MIN(1) +               
  4.            PROMPT('Remote System')                               
  5.                                                                                 
  6. PARM       KWD(FACILITY) TYPE(*DEC) LEN(2) RANGE(0 23) +           
  7.            MIN(1) PROMPT('Facility')                             
  8.                                                                                 
  9. PARM       KWD(Severity) TYPE(*DEC) LEN(2) RANGE(0 7) +            
  10.            MIN(1) PROMPT('Severity')                             
  11.                                                                                 
  12. PARM       KWD(MSG) TYPE(*CHAR) LEN(1000) MIN(1) +                 
  13.            PROMPT('Message to Send')                             
  14.  
  15. ----
  16.  
  17.      H DFTACTGRP(*NO) ACTGRP(*NEW)                                              
  18.      H BNDDIR('QC2LE')                                                          
  19.                                                                                 
  20.      D/copy qrpglesrc,socket_h                                                  
  21.      D/copy qrpglesrc,sockutil_h                                                
  22.      D/copy qrpglesrc,errno_h                                                   
  23.                                                                                 
  24.      D translate       PR                  ExtPgm('QDCXLATE')                   
  25.      D   length                       5P 0 const                                
  26.      D   data                     32766A   options(*varsize)                    
  27.      D   table                       10A   const                                
  28.                                                                                 
  29.      D gethostname     PR                  extproc('gethostname')               
  30.      D   localname                     *   value                                
  31.      D   localnamelen                10I 0 value                                
  32.                                                                                 
  33.      D die             PR                                                       
  34.      D   peMsg                      256A   const                                
  35.                                                                                 
  36.      D sock            S             10I 0                                      
  37.      D err             S             10I 0                                      
  38.      D len             S             10I 0                                      
  39.      D bindto          S               *                                        
  40.      D addr            S             10U 0                                      
  41.      D buf             S           1000A                                        
  42.      D buflen          S             10I 0                                      
  43.      D host            S            128A                                        
  44.      D localhost       S            128A                                        
  45.      D priority        S             10I 0                                      
  46.      D facility        S              2P 0                                      
  47.      D severity        S              2P 0                                      
  48.      D msg             S           1000A                                        
  49.      D destlen         S             10I 0                                      
  50.      D destaddr        S               *                                        
  51.      D months          S              3A    dim(12) ctdata perrcd(12)           
  52.                                                                                 
  53.      c     *entry        plist                                                  
  54.      c                   parm                    host                           
  55.      c                   parm                    facility                       
  56.      c                   parm                    severity                       
  57.      c                   parm                    msg                            
  58.                                                                                 
  59.      c                   eval      *inlr = *on                                  
  60.                                                                                 
  61.      C* Get the 32-bit network IP address for the host                          
  62.      c                   eval      addr = inet_addr(%trim(host))                
  63.      c                   if        addr = INADDR_NONE                           
  64.      c                   eval      p_hostent = gethostbyname(%trim(host))       
  65.      c                   if        p_hostent = *NULL                            
  66.      c                   callp     die('Unable to find that host!')             
  67.      c                   return                                                 
  68.      c                   endif                                                  
  69.      c                   eval      addr = h_addr                                
  70.      c                   endif                                                  
  71.                                                                                 
  72.      C* Create a UDP socket:                                                    
  73.      c                   eval      sock = socket(AF_INET: SOCK_DGRAM:           
  74.      c                                       IPPROTO_IP)                        
  75.      c                   if        sock < 0                                     
  76.      c                   callp     die('socket(): '+%str(strerror(errno)))      
  77.      c                   return                                                 
  78.      c                   endif                                                  
  79.                                                                                 
  80.      C* Create a socket address struct with destination info                    
  81.      C                   eval      destlen = %size(sockaddr_in)                 
  82.      c                   alloc     destlen       destaddr                       
  83.      c                   eval      p_sockaddr = destaddr                        
  84.                                                                                 
  85.      c                   eval      sin_family = AF_INET                         
  86.      c                   eval      sin_addr = addr                              
  87.      c                   eval      sin_port = 514                               
  88.      c                   eval      sin_zero = *ALLx'00'                         
  89.                                                                                 
  90.      C* Calc Priority, and start to load message                                
  91.      c                   eval      priority = (facility*8)+severity             
  92.      c                   eval      buf = '<' + %char(priority) + '>'            
  93.                                                                                 
  94.      c* load date/time in syslog format                                         
  95.      c                   eval      buf = %trimr(buf)                            
  96.      c                             + months(%subdt(%date():*M))                 
  97.      c                             + ' '                                        
  98.      c                             + %char(%subdt(%date():*D))                  
  99.      c                             + ' '                                        
  100.      c                             + %char(%time():*hms:)                       
  101.                                                                                 
  102.      C* load system name                                                        
  103.      c                   callp     gethostname(%addr(localhost)                 
  104.      c                             : %size(localhost))                          
  105.      c                   eval      buf = %trimr(buf)                            
  106.      c                             + ' '                                        
  107.      c                             + %str(%addr(localhost))                     
  108.                                                                                 
  109.      c* load message                                                            
  110.      c                   eval      buf = %trimr(buf)                            
  111.      c                             + ' '                                        
  112.      c                             + msg                                        
  113.      c                   eval      buflen = %len(%trimr(buf))                   
  114.                                                                                 
  115.      C* Convert to ASCII                                                        
  116.      c                   callp     translate(buflen: buf: 'QTCPASC')            
  117.                                                                                 
  118.      C* Send the datagram                                                       
  119.      c                   if        sendto(sock: %addr(buf): buflen: 0:          
  120.      c                               destaddr: destlen) < 0                     
  121.      c                   eval      err = errno                                  
  122.      c                   callp     close(sock)                                  
  123.      c                   callp     die('sendto(): '+%str(strerror(err)))        
  124.      c                   return                                                 
  125.      c                   endif                                                  
  126.                                                                                 
  127.      C* end                                                                     
  128.      c                   callp     close(sock)                                  
  129.      c                   return                                                 
  130.                                                                                 
  131.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++          
  132.       *  This ends this program abnormally, and sends back an escape.           
  133.       *   message explaining the failure.                                       
  134.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++          
  135.      P die             B                                                        
  136.      D die             PI                                                       
  137.      D   peMsg                      256A   const                                
  138.                                                                                 
  139.      D SndPgmMsg       PR                  ExtPgm('QMHSNDPM')                   
  140.      D   MessageID                    7A   Const                                
  141.      D   QualMsgF                    20A   Const                                
  142.      D   MsgData                    256A   Const                                
  143.      D   MsgDtaLen                   10I 0 Const                                
  144.      D   MsgType                     10A   Const                                
  145.      D   CallStkEnt                  10A   Const                                
  146.      D   CallStkCnt                  10I 0 Const                                
  147.      D   MessageKey                   4A                                        
  148.      D   ErrorCode                32766A   options(*varsize)                    
  149.                                                                                 
  150.      D dsEC            DS                                                       
  151.      D  dsECBytesP             1      4I 0 INZ(256)                             
  152.      D  dsECBytesA             5      8I 0 INZ(0)                               
  153.      D  dsECMsgID              9     15                                         
  154.      D  dsECReserv            16     16                                         
  155.      D  dsECMsgDta            17    256                                         
  156.                                                                                 
  157.      D wwMsgLen        S             10I 0                                      
  158.      D wwTheKey        S              4A                                        
  159.                                                                                 
  160.      c                   eval      wwMsgLen = %len(%trimr(peMsg))               
  161.      c                   if        wwMsgLen<1                                   
  162.      c                   return                                                 
  163.      c                   endif                                                  
  164.                                                                                 
  165.      c                   callp     SndPgmMsg('CPF9897': 'QCPFMSG   *LIBL':      
  166.      c                               peMsg: wwMsgLen: '*ESCAPE':                
  167.      c                               '*PGMBDY': 1: wwTheKey: dsEC)              
  168.                                                                                 
  169.      c                   return                                                 
  170.      P                 E                                                        
  171.                                                                                 
  172.       /define ERRNO_LOAD_PROCEDURE                                              
  173.       /copy qrpglesrc,errno_h                                                   
  174.                                                                                 
  175. **CTDATA months                                                                 
  176. JanFebMarAprMayJunJulAugSepOctNovDec                                            
  177.  
© 2004-2019 by midrange.com generated in 0.008s valid xhtml & css