CA Service Management

 View Only
  • 1.  Create text file to attach to a ticket

    Posted Apr 18, 2018 01:56 PM

    Hi guys,

    Has anyone here ever had to export the contents of a Spel string variable into a .txt file?

    Unfortunately I have not yet been able to use the native 'expand' function to generate a file on the server. I can only read the contents of a file via Spel.

    I know it is possible to do this using the native 'exec' function with the help of the Windows command 'CMD / C ECHO', but this alternative is not very efficient when multiple application servers from different operating systems are used.

    I need to be able to do this to later attach this newly created file to a specific ticket.

     

    Has anyone here already overcome this challenge?



  • 2.  Re: Create text file to attach to a ticket

    Posted Apr 18, 2018 02:32 PM

    Hi Daniel,

    as you remember you can use Perl (and call Perl methods) using spel. And Perl as programming language was designed to work with text so this could be a starting point.

    Going further here is a way to upload and attach file using spel: SPEL: How to upload attachment using SPEL 

    Regards,

    Timur



  • 3.  Re: Create text file to attach to a ticket

    Posted Apr 20, 2018 11:28 AM

    cdtj

    Look this!

    Resolved!

    #!/opt/CAisd/bin/pdm_perl -w
    ########################################################################
    ## Modulo:          z_functions.pm
    ## Autor:          Daniel Becker Bighelini
    ## Criado em:          13/06/2016
    ## Modificado por:     Daniel Becker Bighelini
    ## Modificado em:     18/04/2018
    ########################################################################
    use pdm_perlext;
    use pdm_misc;
    use strict;
    use warnings;
    use Env qw(NX_ROOT);
    use Getopt::Std;
    use Encode;
    use utf8;
    use File::Spec;

    sub z_who_am_i  { ( caller(1) )[3] }
    sub z_whow_as_i { ( caller(2) )[3] }

    sub api::z_save_file
    {
         ########################################################################
         ## Metodo:          api::z_save_file
         ## Autor:          Daniel Becker Bighelini
         ## Modificado em:     18/04/2018
         ## Descricao:          Exporta o conteudo de uma variavel para dentro de um arquivo
         ## Parametros:
         ##               zs_str               Variavel string
         ##               zs_filepath          Caminho e nome do arquivo que devera ser criado
         ##               zi_depurar          [OPCIONAL] Nivel de depuracao da funcao 0-nenhum / 1-resumido / 2-detalhado)
         ##               zs_metodo          [OPCIONAL] Nome do metodo que sera identificado no bloco
         ##
         ## Exemplos:
         ##               send_wait(0, top_object(), "call_attr", "api", "z_save_file", zs_str, zs_filepath, zi_depurar, zs_metodo);
         ##               send_wait(0, top_object(), "call_attr", "api", "z_save_file", zs_str, zs_filepath);
         ########################################################################

         # Capturando os parametros
         my $zs_str = $_[0];
         my $zs_filepath = $_[1];
         my $zi_depurar = $_[2];
         my $zs_metodo = $_[3] . " " . z_who_am_i;

         ## Entrada para debug mode
         if ($zi_depurar > 0) {
              z_bloco_inicio($zi_depurar, $zs_metodo);
              log_nx($LOG_SIGNIFICANT, "$zs_metodo Quant. de argumentos : " . @_);
              my @args = @_;
              foreach my $i (0 .. $#args) {
                   log_nx($LOG_SIGNIFICANT, "$zs_metodo ARG #$i $args[$i]");
              }
         } else {
              log_nx($LOG_SIGNIFICANT, "$zs_metodo $zs_filepath");
         }

         #######################################################################################
         ## Inicio do codigo

         log_nx($LOG_SIGNIFICANT, "$zs_metodo Salvando o arquivo '$zs_filepath'...") if $zi_depurar;
         open(my $fh, '>', $zs_filepath) or do {
              log_nx($LOG_ERROR, "$zs_metodo ERRO ao salvar arquivo: '$!'");
              set_return_data(16, $!, 0, 0);
              set_error(1);
              return;
         };
         print $fh "$zs_str";
         close $fh;
         
         my $zs_abs_path = File::Spec->rel2abs($zs_filepath);
         log_nx($LOG_SIGNIFICANT, "$zs_metodo Arquivo '$zs_abs_path' salvo com sucesso!") if $zi_depurar;
         set_return_data(16, $zs_abs_path, 0, 0);
         
         z_bloco_fim($zi_depurar, $zs_metodo);
         ## Fim do codigo
         ###################################
    }


    sub api::z_delete_file
    {
         ########################################################################
         ## Metodo:          api::z_delete_file
         ## Autor:          Daniel Becker Bighelini
         ## Modificado em:     18/04/2018
         ## Descricao:          Exclui um ou mais arquivos.
         ## Parametros:
         ##               zs_filepath          Caminho e nome do arquivo que devera ser excluido
         ##               zi_depurar          [OPCIONAL] Nivel de depuracao da funcao 0-nenhum / 1-resumido / 2-detalhado)
         ##               zs_metodo          [OPCIONAL] Nome do metodo que sera identificado no bloco
         ##
         ## Exemplos:
         ##               send_wait(0, top_object(), "call_attr", "api", "z_delete_file", zs_filepath, zi_depurar, zs_metodo);
         ##               send_wait(0, top_object(), "call_attr", "api", "z_delete_file", zs_filepath);
         ########################################################################

         # Capturando os parametros
         my $zs_filepath = $_[0];
         my $zi_depurar = $_[1];
         my $zs_metodo = $_[2] . " " . z_who_am_i;

         ## Entrada para debug mode
         if ($zi_depurar > 0) {
              z_bloco_inicio($zi_depurar, $zs_metodo);
              log_nx($LOG_SIGNIFICANT, "$zs_metodo Quant. de argumentos : " . @_);
              my @args = @_;
              foreach my $i (0 .. $#args) {
                   log_nx($LOG_SIGNIFICANT, "$zs_metodo ARG #$i $args[$i]");
              }
         } else {
              log_nx($LOG_SIGNIFICANT, "$zs_metodo $zs_filepath");
         }

         #######################################################################################
         ## Inicio do codigo

         log_nx($LOG_SIGNIFICANT, "$zs_metodo Excluindo o arquivo '$zs_filepath'...") if $zi_depurar;
         my $zi_removed = unlink($zs_filepath);
         
         if ($zi_removed eq 0) {
              log_nx($LOG_ERROR, "$zs_metodo ERRO ao excluir o arquivo '$zs_filepath'.");
              set_return_data(16, 'NOK', 0, 0);
              set_error(1);
              return;

         } else {
              log_nx($LOG_SIGNIFICANT, "$zs_metodo $zi_removed arquivo(s) excluido(s) com sucesso!") if $zi_depurar;
              set_return_data(16, 'OK', 0, 0);
         }
         
         z_bloco_fim($zi_depurar, $zs_metodo);
         ## Fim do codigo
         ###################################
    }


    sub z_bloco_inicio
    {
         ########################################################################
         ## Metodo:          z_bloco_inicio
         ## Autor:          Daniel Becker Bighelini
         ## Modificado em:     20/05/2016
         ## Descricao:          Grava em log o inicio de bloco de depuracao
         ## Parametros:
         ##               zi_depurar          Nivel de depuracao da funcao 0-nenhum / 1-resumido / 2-detalhado)
         ##               zs_metodo          Nome do metodo que sera identificado no bloco
         ##
         ## Exemplos:
         ##               z_bloco_inicio(1, "z_calcula_data")
         ########################################################################
         #######################################################################################
         ## Inicio do codigo
         my $zi_depurar = shift;
         my $zs_metodo = shift;

         if ($zi_depurar > 0) {
              log_nx($LOG_SIGNIFICANT, "=" x 60);
              log_nx($LOG_SIGNIFICANT, "$zs_metodo Inicio : " . localtime());
              #log_nx($LOG_SIGNIFICANT, "$zs_metodo Usuario : ");
              log_nx($LOG_SIGNIFICANT, "=" x 60);
         }
         ## Fim do codigo
         ###################################
    }


    sub z_bloco_fim
    {
         ########################################################################
         ## Metodo:          z_bloco_fim
         ## Autor:          Daniel Becker Bighelini
         ## Modificado em:     13/06/2016
         ## Descricao:          Grava em log o fim de bloco de depuracao
         ## Parametros:
         ##               zi_depurar          Nivel de depuracao da funcao 0-nenhum / 1-resumido / 2-detalhado)
         ##               zs_metodo          Nome do metodo que sera identificado no bloco
         ##
         ## Exemplos:
         ##               z_bloco_fim(1, "z_calcula_data")
         ##
         ########################################################################
         #######################################################################################
         ## Inicio do codigo
         my $zi_depurar = shift;
         my $zs_metodo = shift;
         
         if ($zi_depurar > 0) {
              log_nx($LOG_SIGNIFICANT, "=" x 60);
              log_nx($LOG_SIGNIFICANT, "$zs_metodo Fim");
              log_nx($LOG_SIGNIFICANT, "=" x 60);
         }
         ## Fim do codigo
         ###################################
    }

    # perl packages must end with a true statement!
    1;


  • 4.  Re: Create text file to attach to a ticket

    Posted Apr 18, 2018 07:20 PM

    Hello Daniel,

     

    If you have PAM you could use EXEC to execute a ITPAM macro and the PAM process will create the file and attach it to the specific case.

     

    Regards,



  • 5.  Re: Create text file to attach to a ticket
    Best Answer

    Posted Apr 20, 2018 12:49 PM

    Unfortunetly i dont use PAM in my organization.

    I resolved with PERL function.

    Thanks.