Re: [firebase-br] Qtde de usuários conectados na base

Valdir Marcos valdir.marcos em ig.com.br
Qua Out 12 12:31:23 -03 2005


Até o FB 1.5, não.
Vc precisa fazer um programinha em C/C++ ou Delphi para obter essa 
informação.
Veja dois exemplos no final desse email.

----- Original Message ----- 
From: "Tiago L. Moretto" <tiago29 em gmail.com>
To: <lista em firebase.com.br>
Sent: Tuesday, October 11, 2005 11:59 PM
Subject: [firebase-br] Qtde de usuários conectados na base


Gostaria de saber se existe algum "Select" nas tabelas do sistema do
Firebird que me retorne a qtde de usuários conectados na base.
 Grato.




------------
Delphi 7
------------
unit UPrincipal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IBDatabaseInfo;

type
  TfrmPrincipal = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    sCodEmp: string;
  end;

var
  frmPrincipal: TfrmPrincipal;
  Ambiente: record
              Servidor: string;
              CodEmp: string;
              Empresa: string;
              Fantasia: string;
              Cidade: string;
              UF: string;
              CodUsu: string;
              Usuario: string;
              Nivel: string;
              Tipo: string;
              Inicio: string;
              CodLic: string;
              Licenca: string;
              Navegadores: string;
              CodAce: string;
              CodAlu: string;
              DataRecibo: string;
            end;
  HDetalhe: record
              sSystem: string;
              sDB: string;
              sNumber: string;
              sType: string;
              sDate: string;
              sMajorVersion: string;
              sMinorVersion: string;
              sRelease: string;
              sBuild: string;
              sObservation: string;
            end;

implementation

uses UdmPrincipal;

{$R *.dfm}

procedure TfrmPrincipal.FormCreate(Sender: TObject);
begin
  Caption := 'Homeostase - Usuários Conectados';
  Application.Title := 'Homeostase';

  if dmPrincipal = nil then
    dmPrincipal := TdmPrincipal.Create(self);
  dmPrincipal.idbPrincipal.Open;  // Componente IBDataBase
  dmPrincipal.itrPrincipal.Active := True; // Componente IBTransaction
end;

procedure TfrmPrincipal.FormShow(Sender: TObject);
var
  i: integer;
  sUsuarios: string;
begin
  with dmPrincipal.idbiPrinicpal do
  begin
    sUsuarios := 'Users: ' + #13 + #10;
    for i := 0 to UserNames.Count - 1 do
      sUsuarios :=  sUsuarios + UserNames[i] + #13 + #10;
    sUsuarios :=  sUsuarios + #13 + #10;
    sUsuarios :=  sUsuarios + 'Quantity: ' + IntToStr(UserNames.Count) + #13 
+ #10;
    sUsuarios :=  sUsuarios + #13 + #10;
    sUsuarios :=  sUsuarios + #13 + #10;
    sUsuarios :=  sUsuarios + 'DBFileName: '    + DBFileName + #13 + #10;
    sUsuarios :=  sUsuarios + 'Fetches: '       + IntToStr(Fetches) + #13 + 
#10;
    sUsuarios :=  sUsuarios + 'CurrentMemory: ' + IntToStr(CurrentMemory);
    Memo1.Lines.Text := sUsuarios;
  end;
end;

procedure TfrmPrincipal.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  dmPrincipal.idbPrincipal.Close;
end;

end.
------------




------------
C/C++
-----------
#include "ibase.h"
#include <cstdlib>
#include <iostream>

using namespace std;

/*
 * Name: getConnectedUsers.c
 *
 *
 * It then uses the isc_database_info call to fetch the number
 * of users connected to that database
 *
 *  Brett Bandy, June 1998
 */

void usage()
{
  printf("Syntax Error:n");
  printf("t getConnectedUsers n");
  return;
}

int main(int argc, char **argv)
{
  ISC_STATUS isc_status[20];
  isc_db_handle db;
  char dbname[128];
  char infoBuffer[] = {isc_info_user_names};
  char result_buffer[512];
  char *p;
  char userName[32];
  int length;
  char item;
  int i;
  int userNameSize;
  int totalUsers;

  /* init dbhandle */
  db = 0L;

  switch(argc)
  {
    case 1:
      /* get dbname from user */
      printf("Enter Database to check for connections: ");
      gets(dbname);
      break;
    case 2:
      /* dbname is passed in */
      strcpy(dbname, argv[1]);
      break;
    default:
 usage();
 return 0 ;
  }


  printf("nAttempting to attach to database:nt%snn", dbname);

  isc_attach_database(isc_status, strlen(dbname), dbname,
                      &db, 0, NULL);
  if(isc_status[0] == 1 && isc_status[1])
  {
    isc_print_status(isc_status);
    exit(1);
  }

  /* now fetch the number of cache buffers database is using */
  isc_database_info(isc_status,
      &db,                  /* ptr to db handle */
      sizeof(infoBuffer),   /* size of items buffer */
      infoBuffer,           /* array of items to retrieve */
      sizeof(result_buffer),
      result_buffer);       /* results will be written */
  if(isc_status[0] == 1 && isc_status[1])
  {
    isc_print_status(isc_status);
    exit(1);
  }

  /* initialize totalUsers */
  totalUsers = 0;

  /*
   * Format for this call is:
   *
   *  1 byte:  type of info item (isc_info_user_names)
   * 2 bytes: length of next item (must use isc_vax_integer
   *             to get correct byte order)
   *  x bytes: next item which consists of...
   *  1 byte:  length of username
   *  x bytes: username
   *
   */

  /* extract the info from result_buffer */
  /* loop because there can be more that one user */
  for(p = result_buffer; *p != isc_info_end;)
  {

    item = *p++;   /* item return type */

    if(item != isc_info_user_names)
    {
      printf("Error retrieving user namesn");
      return 0;
    }

    /* this will return the length of the */
    /* item in bytes (length is 2 byte) */
    length = isc_vax_integer(p, 2);

    /* increment pointer so points to next username */
    p+=2;

    /* username items are stored in pascal style
       <1 byte for length> */

    /* get length of username */
    userNameSize = isc_vax_integer(p,1);
    p+=1;

    /* copy username from result_buffer to userName char array */
    memcpy(userName, p, length);
    userName[userNameSize] = '0';

    /* advance p to point to start of next item */
    p+=userNameSize;

    printf("User [%s] is connectedn", userName);
    totalUsers++;
  }

  printf("nTotal Connected User Count is %dn", totalUsers);

  isc_detach_database(isc_status, &db);
  if(isc_status[0] == 1 && isc_status[1])
  {
    isc_print_status(isc_status);
    exit(1);
  }
}

-----------





Mais detalhes sobre a lista de discussão lista